• Post Reply Bookmark Topic Watch Topic
  • New Topic

null pointer error with calling an array from multiple methods  RSS feed

 
Hamza Fetuga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! While I was trying to solve this problem, www.codechef.com/problems/SUMTRIAN but I keep getting nullpointerexception error and I'm certain it has to do with getting the elements present in an array in a method different from the one it was created in. I'm pretty sure my algorithm is right. Here's my code



Please i really need help on it. Thanks
 
Tony Docherty
Bartender
Posts: 3264
81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the full error message and stack trace.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hamza Fetuga wrote:Hey guys! While I was trying to solve this problem, www.codechef.com/problems/SUMTRIAN but I keep getting nullpointerexception error and I'm certain it has to do with getting the elements present in an array in a method different from the one it was created in. I'm pretty sure my algorithm is right. Here's my code


In main you are not creating, initializing, and filling the instance variable, you are creating a new method local variable which has the same name (and so hides the instance variable). So later, when you try to use the instance variable you get nulls because it hasn't been initialized or filled.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You create a new sumtrial object in line 19.

You're not initializing the member variable b (declared in line 9) of that object anywhere, so it is null.

Note: the b[][] that you declare and initialize in line 20 is a local member variable, which has nothing to do with the member variable of the sumtrial object.

In line 33 you call str.getstore(0,0);, then you get to line 61, where a NullPointerException happens because the member variable b of the sumtrial object is null.
 
Hamza Fetuga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>javac sumtrial.java
>Exit code: 0
>java -cp . sumtrial
4
1
1 2
3 1 2
1 3 2 2
Exception in thread "main" java.lang.NullPointerException
at sumtrial.getstore(sumtrial.java:63)
at sumtrial.main(sumtrial.java:33)
>Exit code: 1
 
Hamza Fetuga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where should I create the array such that its constituents are available to all other methods. For integers and strings its pretty easy but this arrays keep giving me problems.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you could try removing the 'int' keyword on line 20 which would then use the member one instead.
 
Hamza Fetuga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just did that and I'm getting a couple of errors all from that line.
Error: not a statement
Error: ; expected
Error: ] expected
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you remove the int keyword on line 20, you also have to remove the square brackets after the variable name.
 
Hamza Fetuga
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Its working! Thanks a lot guys :*
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!