• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static error and NullPointerException with Array  RSS feed

 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an arraylist, list, that I need to take the size to declare an array, arr3, in another class. Problem 1: the size is giving me an error of "can not make static reference to non-static method getListSize() from the type FindItemInfo". I tried to change getListSize() to static and it gives me and error another place. I try to then fix that, and I get a new error... and so on.

I am using arr3 to store items the user can not afford. I have this next problem even when I input an integer for the arr3 size. I am getting a NullPointerException, and I can't figure out why the arr3[] is not loading. I tried debugging but can not figure out where I went wrong, especially since this code was working in the last assignment before I changed list[] to an arrayList. It always breaks at line 64, but I believe it has to be somewhere in the cashOut() method.

Relevant code is:

 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is saying call the getListSize static method in the FindItemInfo class but there isn't a static method called getListSize in that class. There is an instance method though which can be called from a reference to a instance of the class.

It's hard to say how best to fix your code without knowing what it is supposed to be doing but one solution would be for the TransactionCalc class to have a constructor that takes an instance of the FindItemInfo class and create the array in the constructor. This would only work though if the list size doesn't change after the instance of the TransactionCalc is created.

Why are you creating and storing a new instance of ItemAttribute and then immediately overwriting it with item?

Why does this method have a parameter arr3 when there is an instance variable with the same name?
 
Larissa Perkins
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The null pointer exception is now all set. It was the combination of reassigning it and having it as a parameter. I left some code in as I was trying to debug from before. Thanks!

Tony Docherty wrote:That is saying call the getListSize static method in the FindItemInfo class but there isn't a static method called getListSize in that class. There is an instance method though which can be called from a reference to a instance of the class.


I have changed the code to now be



but the code is breaking still breaking at the same place. However, the error is not an out of bounds exception. I went to findItemInfo class and checked that getListSize() gives the correct value, and it does. When I do this.size.getListSize() in the FindItemInfo class, it gives me 0. Why and I not getting 8?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Larissa Perkins wrote:I went to findItemInfo class and checked that getListSize() gives the correct value, and it does. When I do this.size.getListSize() in the FindItemInfo class, it gives me 0. Why and I not getting 8?

Because each instance is unique. You have created a new instance of FindItemInfo and are then querying its size which will be 0 because you haven't done anything with the instance to add anything to the list. As I said in my previous post you need to pass in the instance of the FindItemInfo class that contains the data you want your TransactionCalc instance to process.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!