Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ok, So I thought I understood nullpointerexception when I was working on C++, but now that I am making this class in Java, I obviously didn't understand as much as I thought because I am so confused what I did so wrong. I have changed 20 things and back and can't figure it out. This is the original version I had, can anyone tell me where I am making the error in my code?

Java
<pre>
public class HamadHamdi {

	static MyString s1, s2, s3, s4;
	static void setup() {
		char[] c1 = {'h', 'e', 'l', 'l', 'o'};
		char[] c2 = {'h', 'e', 'l'};
		char[] c3 = {'H'};
		char[] c4 = {};
		s1 = new MyString(c1);
		s2 = new MyString(c2);
		s3 = new MyString(c3);
		s4 = new MyString(c4);
	}
	static void assertTrue(boolean b) {
		if (!b) {
			throw new AssertionError();
		}
	}
	static void assertFalse(boolean b) {
		if (b) {
			throw new AssertionError();
		}
	}
	static void testLength() {
		assertTrue(s1.length() == 5);
		assertTrue(s2.length() == 3);
		assertTrue(s3.length() == 1);
		assertTrue(s4.length() == 1);
	}
	static void testEquals() {
		Integer a = new Integer(123);
		char c[] = {'h', 'e', 'l'};
		MyString s = new MyString(c);
		assertTrue(s.equals(s2));
		assertFalse(s1.equals(s2));
		assertFalse(s3.equals(a));
	}
	public static void main(String[] args) {
		testLength();
		testEquals();
	}
}
class MyString implements Comparable<MyString> {
	private char[] data;
	
	public MyString(char[] chars) {
		this.data = new char[chars.length];
		for (int i = 0; i < chars.length; i++) {
			this.data[i] = chars[i];
		}
	}
	public char charAt(int i) {
		return data[i];
	}
	public int compareTo(MyString other) {
		int limit = Math.min(other.length(), length());

        char[] tempArr = other.data;
        int i = 0;
        while (i < limit) {
            char ch1 = charAt(i);
            char ch2 = tempArr[i];
            if (ch1 != ch2) {
                return ch1 - ch2;
            }
            i++;
        }

        return length() - other.length();
	}
	public boolean equals(MyString other) {
		boolean isEqual = true;
		if (other.length() != data.length)
			isEqual = false;
		else {
			for (int i = 0; i < data.length; i++) {
				if (other.data[i] != this.data[i])
					isEqual = false;
			}
		}
		return isEqual;
	}
	public boolean equalsIgnoreCase(MyString other) {
		MyString otherLowerCase = other.toLowerCase();
		MyString thisLowerCase = new MyString(data).toLowerCase();
		return thisLowerCase.equals(otherLowerCase);
		
	}
	public int length() {
		return data.length;
	}
	public void output() {
		System.out.println(data);
	}
	public MyString substring(int begin) {
		return substring(begin, data.length);
	}
	public MyString substring(int begin, int end) {
		int length = end - begin;
		char[] tempArr = new char[length];
		for (int i = 0; i < (length); i++) {
			tempArr[i] = data[begin];
			begin++;
		}
		return new MyString(tempArr);
	}
	public MyString toLowerCase() {
		char[] tempArr = new char[data.length];
		for (int i = 0; i < data.length; i++) {
			if (data[i] >= 'A' && data[i] <= 'Z') {
				tempArr[i] = (char)(data[i] + 32);
			}
			else
				tempArr[i] = data[i];
		}
		return new MyString(tempArr);
	}
	public MyString toUpperCase() {
		char[] tempArr = new char[data.length];
		for (int i = 0; i < data.length; i++) {
			if (data[i] >= 'a' && data[i] <= 'z') {
				tempArr[i] = (char)(data[i] - 32);
			}
			else
				tempArr[i] = data[i];
		}
		return new MyString(tempArr);
	}
	public static MyString valueOf(int i) {
		char[] tempArr = Integer.toString(i).toCharArray();
		return new MyString(tempArr);
	}
}


What I have tried:

I tried changing the constructor, but I do not know if that is what I need to do. Any help is welcome.
Posted
Updated 18-Nov-20 23:02pm
Comments
Richard Deeming 19-Nov-20 4:46am    
Are we supposed to guess on which line the error occurs?

Details here:
Class NullPointerException[^]
Java error java.lang.nullpointerexception[^]

DEBUG and look for the potential place and fix/handle it.
Reference: How to resolve the java.lang.NullPointerException[^]
Quote:
In Java, the java.lang.NullPointerException is thrown when a reference variable is accessed (or de-referenced) and is not pointing to any object. This error can be resolved by using a try-catch block or an if-else condition to check if a reference variable is null before dereferencing it.




Debug and see why that is null. Set it up correctly and handle null condition.
Your case, your main calls for: testLength();
It checks for:
Java
assertTrue(s1.length() == 5);

s1 is not defined yet. Think you missed calling setup before calling test methods. Make sure strings are defined before using properties to that object.


For Debug, if needed, look:
jdb - The Java Debugger[^]
Debugging the Eclipse IDE for Java Developers | The Eclipse Foundation[^]
 
Share this answer
 
You simply forgot to call setup in the main method.
Change from
Quote:
public static void main(String[] args) {
testLength();
testEquals();
}
to
Java
public static void main(String[] args) {
        setup();
		testLength();
		testEquals();
	}
 
Share this answer
 
Comments
hammock1 19-Nov-20 5:56am    
I feel so stupid... No clue how I missed that. Thank you. It has been a long day.
CPallini 19-Nov-20 13:18pm    
It happens, don't bother. You are welcome.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900