• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to read data from a text file into an array and meeting a null pointer issue. I can see that the readDescription does indeed contain the String I have in the text file but when I assign this to the array (map[x][y].description) I get a NullPointerException notice.

It'll be obvious but cannot see the issue.

 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What line do you get the NullPointerException on?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You never actually instantiated any DefMap objects.

Henry
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:What line do you get the NullPointerException on?


Line 50 and, obviously, the calling one 24.
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:You never actually instantiated any DefMap objects.

Henry


Line 7?
 
Tim Cooke
Marshal
Posts: 3873
233
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Henry said.

Line 7 creates an array of arrays of Type DefMap. The arrays are empty. That is, each array element contains null.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Newport wrote:
Henry Wong wrote:You never actually instantiated any DefMap objects.


Line 7?


Line 7 creates one array of DefMap arrays object, and nine Defmap array objects. It doesn't create any DefMap objects.

Henry
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Gary Newport wrote:
Henry Wong wrote:You never actually instantiated any DefMap objects.


Line 7?


Line 7 creates one array of DefMap arrays object, and nine Defmap array objects. It doesn't create any DefMap objects.

Henry


DOH! Of course! Sorry, told you it was bound to be simple. Thank you.
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Gary Newport wrote:
Henry Wong wrote:You never actually instantiated any DefMap objects.


Line 7?


Line 7 creates one array of DefMap arrays object, and nine Defmap array objects. It doesn't create any DefMap objects.

Henry


Sorry. Really going mad here. How do I instantiate it? Obviously initially assigning a value to it within a method does not work (since the null Pointer arises) but I am struggling to recall what I do when and where.

Actually I am struggling to recall anything and really should not be coding today but I need to take my mind off of something and therefore this seemed a good idea - not one of my best, tbh.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myArray = new Foo[123];
That creates a new array containing 123 spaces which are to be filled with Foo instances. 123 different instances or 123 copies of the same instance: either is permissible. But until such time as you put Foo instances in there, the array is full of nulls.
System.out.println(Arrays.toString(myArray));
That will print [null, null, … null]
myArray = new Foo[]{foo1, foo2, foo3, ...};
That will fill your array with Foo instances (as long as those references don't point to null).
myArray = new Foo[]{new Foo(...), new Foo(...), new Foo(...), ...};
That will go one better and fill your array with real Foo instances; you can't get null from new Foo(...).

You can loop through your array (don't use a for‑each loop) and fill all its spaces, or you can create a Stream which can make you an array, as alternative techniques.
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:myArray = new Foo[123];
That creates a new array containing 123 spaces which are to be filled with Foo instances. 123 different instances or 123 copies of the same instance: either is permissible. But until such time as you put Foo instances in there, the array is full of nulls.
System.out.println(Arrays.toString(myArray));
That will print [null, null, … null]
myArray = new Foo[]{foo1, foo2, foo3, ...};
That will fill your array with Foo instances (as long as those references don't point to null).
myArray = new Foo[]{new Foo(...), new Foo(...), new Foo(...), ...};
That will go one better and fill your array with real Foo instances; you can't get null from new Foo(...).

You can loop through your array (don't use a for‑each loop) and fill all its spaces, or you can create a Stream which can make you an array, as alternative techniques.


Thank you. My problem is that I have to initialise each of the properties of my array; since my array is based upon a class definition. I tried looping through but got Null again and I have no doubt you have already explained this above but I am simply not getting it today.

I tried this:



...knowing that this could not possibly work since there is no difference between me doing this and my earlier assignment of a value. Today is NOT a good day for coding.

 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Newport wrote:. . . Today is NOT a good day for coding.
In which case stick to coffee and do it tomorrow. Unless you have already left it n days already 
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gary Newport wrote:
My problem is that I have to initialise each of the properties of my array; since my array is based upon a class definition. I tried looping through but got Null again and I have no doubt you have already explained this above but I am simply not getting it today.



...knowing that this could not possibly work since there is no difference between me doing this and my earlier assignment of a value. Today is NOT a good day for coding.


Well, first of all, since you need DefMap objects, shouldn't you actually create DefMap objects? -- instead of just using an empty string literal (which is not a DefMap object).

And second, and I am sure this was a mistake, the DefMap class is an inner class... but ... if this is not a mistake, you will also need to instantiate an outer class as well.

Henry
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Gary Newport wrote:
My problem is that I have to initialise each of the properties of my array; since my array is based upon a class definition. I tried looping through but got Null again and I have no doubt you have already explained this above but I am simply not getting it today.



...knowing that this could not possibly work since there is no difference between me doing this and my earlier assignment of a value. Today is NOT a good day for coding.


Well, first of all, since you need DefMap objects, shouldn't you actually create DefMap objects? -- instead of just using an empty string literal (which is not a DefMap object).

And second, and I am sure this was a mistake, the DefMap class is an inner class... but ... if this is not a mistake, you will also need to instantiate an outer class as well.

Henry


This is because I AM BEING A MORON! I was playing with the code and forgot to display the original code. I did have:



I'm going to rest this for a while since I am not absorbing anything at present; too many distractions and not enough brain-power.

Thank you for your assistance and once I am on the other side of my concerns I'll look at this again.

Once again, thank you for your time.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use anything.something = somethingElse;
That suggests you have not got all your fields private (as they should be). You should be setting all those fields in the constructor. You may need getXXX and setXXX methods. This is what that loop should look like:-Why you want an object with all 0s and empty description, I don't know. Why are you using 10? You should use myArray.length (presumably map.length and map[i].length). And you should be very careful about using index up to 10 when you have a 9‑element array
 
Gary Newport
Greenhorn
Posts: 18
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Don't use anything.something = somethingElse;
That suggests you have not got all your fields private (as they should be). You should be setting all those fields in the constructor. You may need getXXX and setXXX methods. This is what that loop should look like:-Why you want an object with all 0s and empty description, I don't know. Why are you using 10? You should use myArray.length (presumably map.length and map[i].length). And you should be very careful about using index up to 10 when you have a 9‑element array


Ahh, thank you. The setting of "",0,0,0 was simply to get the idea of initialization right since I really wasn't going anywhere before.

I did know of the .length concept and forgot to use it; shows how bad my day was.

Thank you so much; I'll explore this in a couple of days time when I get back to some form of normality.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!