0

I am creating a method which will take int n as a parameter, convert a previously initialized String into an integer array, add 1 to each index n times, create a new char array from each increment of the int array via casting, then convert all n char arrays into new strings and print them to the screen.

Netbeans throws a NullPointerException in my current project, and I'm not sure why. Could someone explain why this error is present and what I ought to do to fix it?

Relevant Class: https://www.dropbox.com/s/gv02i1traulg8kp/StringShifter.java
Class containing Main Method: https://www.dropbox.com/s/ymon96ovv4c2lnf/CodeBreaker.java

Error:
Exception in thread "main" java.lang.NullPointerException
at Project5.StringShifter.shift(StringShifter.java:35)
at Project5.CodeBreaker.main(CodeBreaker.java:18)

1
  • 8
    Please put the relevant code in the question, rather than in Dropbox. You should edit it to a short but complete program demonstrating the problem. Commented Nov 7, 2013 at 21:36

3 Answers 3

6

Your problem is here:

    int[] a = null; 
    char[] b = null; 
    int r = 0; 

    for (int i = 0; i <= text.length(); i++) { 
        a[i] = text.charAt(i); 
    }

a is set to point to null, and then you try to assign a value to a[i] (which currently does not have a place in memory), which will give you a NullPointer.

Sign up to request clarification or add additional context in comments.

Comments

3

Look at your code:

int[] a = null; 
char[] b = null; 
int r = 0; 

for (int i = 0; i <= text.length(); i++) { 
    a[i] = text.charAt(i); 
}

a is null - you've explicitly set it to null. So

a[i] = text.charAt(i);

will fail with a NullReferenceException - it's bound to. You'd need to initialze a to refer to an array, e.g.

int[] a = new int[text.length()];

You also need to change your upper bound - as when i is text.length(), text.charAt(i) will throw an exception. So you want:

int[] a = new int[text.length()];
for (int i = 0; i < text.length(); i++) { 
    a[i] = text.charAt(i); 
}

It's not clear why you want an int[] at all though. It would be simpler to use:

char[] chars = text.toCharArray();
for (int i = 0; i < text.length(); i++) {
    chars[i] = (char) (chars[i] + 1);
}

Also:

  • There's no point in going over the loop multiple times - just add n instead of 1
  • There's no point in creating a new string on every iteration; just do it at the end

4 Comments

int[] a = new int[a.length()]; Your code is confused. I think you meant what you had previously written... a[i] = new int[text.length()];
@MirroredFate: Yup, just a typo - fixed. (Although it's just assigning to a, not a[i] - that bit was wrong before too!)
Oops, I missed that one too. Is the loop loop multiple also a typo? Because if not I am really confused.
@MirroredFate: Yes, it is. Again, fixed.
0

You define int[] a = null and never initialize it.

When you try to access it in a[i] = text.charAt(i); you get the NullPointerException.

You can solve you issue adding a initialization:

int[] a = new int[text.length];

1 Comment

int[] a = null; does initialize it - it initialize the variable to null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.