• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble with school assignment. Inventory Program  RSS feed

 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, just starting off with I'm a complete beginner and this class has only recently gotten a bit more difficult that its out of my hands for figuring out. I'm sure I have some code that isn't necessary and I know for a fact I am missing something. I can't get this to operate with how I need it to. I have to display each part of the array one by one in a GUI window, with buttons for next and previous, if the program is on the first input and the previous button is pressed it must go to the last input, and vice versa. I am getting errors right around here telling me that it can't find the symbol for MPhone. If I need to expand with more information just ask.



This is my full code.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that you declare MPhone as a local variable in the init() method. That means MPhone is only visible inside the init() method. You need to understand the difference between Java local variables and instance variables.

BTW, the convention in Java is to have variable names start with a lowercase letter.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea i know now that variable names are lowercases, I only recently was told that by another member of this forum last time I reached out. My professor hasn't even said anything about it so I haven't changed it for the duration of the assignment. This class was a bit easier before we started working on GUI's. Ok, so I'm gonna try and see if I can rework it so that the array isn't stuck to the method init()
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Done tons of reading so far, and I cant come across anything to help me make the array an instance variable. I've tried moving it around but it just creates more problems without correcting the initial problem. Is it possible to get some better direction on fixing this problem. Do i have to create a class to put it into, its worked in that same position all the way up until this part of the program so I'm not even sure what to do with it.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rande Fisher wrote:Done tons of reading so far, and I cant come across anything to help me make the array an instance variable. I've tried moving it around but it just creates more problems without correcting the initial problem. Is it possible to get some better direction on fixing this problem. Do i have to create a class to put it into, its worked in that same position all the way up until this part of the program so I'm not even sure what to do with it.


Rande, you got the hint by:
Junilu Lacar wrote:The problem is that you declare MPhone as a local variable in the init() method. That means MPhone is only visible inside the init() method.


What you need to do?
Declare "SmartPhone[] MPhone" as an instance variable, however, initialise "MPhone = new SmartPhone[4]" inside init() method.

Does it helps to understand the problem?
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that's a bit easier to grasp. I stepped away from it to watch the walking dead. Will see if I can fix it after its over. Definitely appreciate the help with this.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I think that problem has been resolved. and an error I ran into after fixing that. Now im running into an issue with the GUI. It only shows in the command prompt the information. I get an error of





Heres what I've gotten after fixing the first issue I had.
 
Mark King
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's happening is that you have an instance variable MPhone, which is initialized by default to null. Then when you try to initialize MPhone within the method, you are instead creating a local variable MPhone and initializing that. Therefore the instance variable remains null, and when you try to access a method on your null object you get NullPointerException
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand what you mean. I did some googling about NPE errors and thats basically what was told. Is there any material you can suggest I read or address in fixing this. In my googling, I've found many fixes but none of which seem to work, or perhaps I'm not doing it correctly.
 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to think that this is what was meant, but I can't be sure. It does change the error that I get, but it more or less seems like it lights up the same spot, but with extra issues. This is the area that was changed up:




The error I get now is:

 
Rande Fisher
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind guys, I greatly appreciate the help. I have figured it out now. You guys were great.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!