• Post Reply Bookmark Topic Watch Topic
  • New Topic

NullPointerException  RSS feed

 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently installed JRE-7 as an update on my Eclipse Europa compiler. Now, two of my previously written programs (an ATM and an Arrays program) do not compile, whereas they did compile perfectly with the previous JRE environment. For example,the JRE-7 throws an Exception in thread in my Arrays program when I enter an incorrect char and my while loop goes into action to catch it, then the Exception occurs when it reaches this line
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That, my friend, is a LOT of code to expect someone to slog through...especially when it is not properly formatted. Please read THIS article on how to properly use code tags when posting your source code. I've copied what you have and done it here - it is MUCH easier for someone to read now.

And since you have three separate class files, I put each in their own set of code tags. You probably have them in separate files, so this way, the line numbers here should match the line numbers your compiler gives you.



 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I recently installed JRE-7 as an update on my Eclipse Europa compiler. Now, two of my previously written programs (an ATM and an Arrays program) do not compile, whereas they did compile perfectly with the previous JRE environment. For example,the JRE-7 throws an Exception in thread in my Arrays program when I enter an incorrect char and my while loop goes into action to catch it, then the Exception occurs when it reaches this line

If the programs no longer compile why are you telling us about runtime problems?
Please post the compiler error messages, if the code does compile then please post the Exception message and stack trace.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To: Fred Rosenberger. Yes, I see what you mean. When I paste, do I hit the "code button" first, then CTRL + V to paste? In the pizza arrays program the problem is on line 36 and in the ATM program the problem is on line 23. In the previous JRE, the programs compiled fine. Now with the JRE-7 Exceptions are thrown on those two lines. I first thought that in JRE-7 that the findInLine() and charAt methods were not supported, however in the pizza arrays program the Exception does not occur on line 30, but when it hits the methods on line 36 the Exception occurs. Thanks for your quick response. I hope this information will narrow the problem down to the above mentioned lines of code.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
If the programs no longer compile why are you telling us about runtime problems?
Please post the compiler error messages, if the code does compile then please post the Exception message and stack trace.


Agreed. Running a program that doesn't compile is not the way to go. The error reported, assuming one is reported, may be completely useless.

http://www.coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication

Henry
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:To: Fred Rosenberger. Yes, I see what you mean. When I paste, do I hit the "code button" first, then CTRL + V to paste? In the pizza arrays program the problem is on line 36 and in the ATM program the problem is on line 23. In the previous JRE, the programs compiled fine. Now with the JRE-7 Exceptions are thrown on those two lines. I first thought that in JRE-7 that the findInLine() and charAt methods were not supported, however in the pizza arrays program the Exception does not occur on line 30, but when it hits the methods on line 36 the Exception occurs. Thanks for your quick response. I hope this information will narrow the problem down to the above mentioned lines of code.

You seem to be confused about compile time and run time problems. If the program won't compile then please post the exact compiler messages. If it does compile and only if it does compile, for the reasons given in the link supplied by Henry, then cut and paste the full Exception message(s) and stack trace(s). Saying an Exception occurs is not enough information to help us to help you fix the problem and the more effort you put into giving us all the information the more likely we are to spend our valuable time helping you.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm confused. You have two classes called NewFoothill. Which one is correct?
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To: Tony Docherty. Here are the outputs from the two programs. The first one is the pizza arrays program. Next the ATM program. These were run with the JRE-7. Welcome to the Foothill
Pizza Shop. Please follow the
directions noted below to select
your pizza size and toppings.
After you have finished making
your size and toppings selections,
the total price will appear. Next,
pay the cashier.

Please enter:
's' for small $8.00
'm' for medium $9.20
'l' for large $10.00
or 'q' to quit T
Incorect selection. Please select again. Exception in thread "main" java.lang.NullPointerException
at NewFoothill.main(NewFoothill.java:36)
Welcome to the Foothill
Pizza Shop. Please follow the
directions noted below to select
your pizza size and toppings.
After you have finished making
your size and toppings selections,
the total price will appear. Next,
pay the cashier.

Please enter:
's' for small $8.00
'm' for medium $9.20
'l' for large $10.00
or 'q' to quit T
Incorect selection. Please select again. Exception in thread "main" java.lang.NullPointerException
at NewFoothill.main(NewFoothill.java:36)

Enter:
'd' to deposit
'w' for withdrawal
'b' for balance
'q' to quit b
New balance: 1000
Enter:
'd' to deposit
'w' for withdrawal
'b' for balance
'q' to quit Exception in thread "main" java.lang.NullPointerException
at NewFoothill.main(NewFoothill.java:23)
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Knute Snortum. There are two programs. 1. pizza arrays, 2. ATM. Each one has a NewFoothill class.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect the problem has to do with the way Scanner works. you should use the hasNextXxx() methods (e.g. hasNextInt().

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:There are two programs. 1. pizza arrays, 2. ATM. Each one has a NewFoothill class.

And thus the confusion.

Please remember when posting that it is your job to make it as EASY AS POSSIBLE for people to help you. Everyone here has limited time, and if they spend it trying to figure out what is what in your post, they have less time to figure out you ACTUAL issue. Many people will just get frustrated and move on.

So...Use code tags. Separate out each file in its own set of code tags. Make it clear what goes with what. Use correct terms - don't talk about run time environments if you are having compiler issues. etc.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Henry Wong. In the JRE-7 the two while loops that I use to trap users into entering correct data in the two programs are causing the Exceptions, I think. I can run the pizza arrays program without a problem if I do not test the while loop with improper data. The ATM program gives an Exception right after I do something. For example, if I deposit $400 the program gives the new balance, then it gives me an Exception even if I am not testing the while loop to catch incorrect data. When I wrote these programs on the previous JRE in my compiler these programs ran perfectly, including the while loops that I use to catch incorrect data. Now I need help on how to make the while loops that catch incorrect data work in the JRE-7 environment.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried the hasNextIn() method?
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:When I wrote these programs on the previous JRE in my compiler ...

You really need to learn to use the correct terminology.
You didn't write these programs in a compiler, you wrote them in Eclipse according to your first post which is an IDE (Integrated Development Environment) which uses a compiler to compile your source code (java files) into bytecode (class files). Any errors that occur during this process are compile time errors. Once you have code that compiles you can run it using the JRE (Java Runtime Environment). Any errors that occur whilst running the program are runtime errors.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Fred Rosenberger: Yes, I understand. My assumption that I could bung all of this together because I believed that the problems were the same in both programs was not the way to go. I will not do this in the future. I was quite upset when I learned that these two programs that I had spent so much time to get right did not work with the JRE-7 update. I thought that Java programs would not have these problems. Again, I was mistaken. I tried using a try/catch block on the while loop that catches the incorrect char input to correct the problem because the one I use on the int input works well. At any rate, my compiler had problems of its own and never finished compiling the pizza arrays program with the new try/catch block installed. Hence, I removed the try/catch block on the while loop that catches incorrect char values. But I do not know if that would have solved the problem. So here I am at the Moose Ranch and it seems that I have upset every one. If you want me to delete all of this, I will. Just instruct me on the right way to get rid of every thing and I will start over again. On the other hand, if you would be pleased that I seek assistance somewhere else, I fully understand. Finally, I would like to tell you that in the past your site has been very helpful to me. Thanks again.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:So here I am at the Moose Ranch and it seems that I have upset every one.

No you haven't upset us - I apologise if we gave you that impression.
I am certainly a little frustrated because in trying to help you we need to know if you have compile time or runtime problems and despite several posts to find out which it is you are still using the wrong terms. Hence my last post to try to make it clear to you how it all fits together. Also I've asked several times for the Exception and stack trace and you still haven't posted it.

As for this not working now in Java 7, I think it's doubtful that anything has changed between Java releases that your code uses that would have made the code not work now.
Can I suggest you post the code for just one of the programs so we can sort one program first and also post the Exception and stack trace so we can see exactly where the problem is occuring.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum: No I have not tried the hasNextInt() because my problem is with the while loop that catches incorrect chars. I have no problem with the int.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:So here I am at the Moose Ranch and it seems that I have upset every one. If you want me to delete all of this, I will. Just instruct me on the right way to get rid of every thing and I will start over again. On the other hand, if you would be pleased that I seek assistance somewhere else, I fully understand. Finally, I would like to tell you that in the past your site has been very helpful to me. Thanks again.


I don't think that you upset anyone, but you certainly aren't helping anyone in order to help you.

1. There is just too much code -- which may or not be related.

2. You keep saying that it is a compile error, yet, you have never posted the compiler errors messages (just vague descriptions).

3. You seems to have ran the program, which doesn't help us figure it out.

4. And the last part, which is completely not fair, but honest nonetheless, you seem to be using your own terminology. This last part, I am guessing is caused by a language barrier.

I know that you are feeling frustrated, but think of it from our point of view. We are trying to help, but we keep getting stuff that we don't want/need, and not getting the simplest of requests, such are the output from the compiler. It is frustrating from our side too...

Henry
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think anyone's upset -- I'm certainly not -- so don't feel bad. Some suggestions have been made to help you ask your question in a way that will get you the help you need faster.

I have been in the software business a long time (about 30 years) and I know that there is a tendency to blame the Nearest Major Event (NME <-- not a real acronym). That is, you upgraded to JRE 7 and then you noticed some problems. It may be the upgrade but evidence currently is against that theory. Let's lay it aside for now.

You are getting runtime errors in your program that point to programming errors that have to do with how you use the Scanner class. Look again at my suggestions. Implement them in one of the programs and post the code. I suspect the other program will need similar changes later.

Good Luck!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just had another look at your code and if the Exception is occurring in the following code:

It's because you need to remove the next line separator that was left by the previous input. You can do this by adding input.nextLine(); at the start of the code block.
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:Knute Snortum: No I have not tried the hasNextInt() because my problem is with the while loop that catches incorrect chars. I have no problem with the int.


Lee, I am not upset -- but you really shouldn't ask for advice and then not take it. I know the problem is with the Scanner object. I could fix it for you, but this is not what the forum is about. It's about you discovering ways to fix your our programs.

Please try my advice and post your new code for one of the programs.

Good Luck!
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To : Tony Docherty I have not done an Exception and stackTrace before. I went to Google to find some code I could work with. No luck, so I will have to go back into my text book and study how to do it. By the way, I just saw your latest post and I took your advice. I hoisted the input.nextLine() in the block and it worked. So now the while loop that catches incorrect chars works again. Hence that program is fine now. I still do not understand why it worked in the previous jre and not in the new one. At any rate, I will open my book up and look into it further. Now the ATM problem needs to be solved, but I think the problem there is different. I am going to call it a night. It is near midnight over here in Thailand and I want to have dinner. Thanks very much for your help.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
lee shotwell wrote:To : Tony Docherty I have not done an Exception and stackTrace before. I went to Google to find some code I could work with. No luck, so I will have to go back into my text book and study how to do it. By the way, I just saw your latest post and I took your advice. I hoisted the input.nextLine() in the block and it worked. So now the while loop that catches incorrect chars works again. Hence that program is fine now.

Great to hear it is working.
The Exception and Stack Trace is what is dumped to the console when things go wrong. I looked back through some posts and noticed it was included when you listed the output of the program - I'd just missed it in amongst all the other text.

lee shotwell wrote:
I still do not understand why it worked in the previous jre and not in the new one. At any rate, I will open my book up and look into it further.

I don't think it would have worked in an earlier version of Java. AFAIK that behaviour has never changed. The thing is do you understand why adding that line makes it work now?

lee shotwell wrote:Thanks very much for your help.
Mai ben lai khrap.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Tony Docherty: I applied the [code = java input.nextLine();] at the top of the blocks of code, as you suggested in order to remove the "next line separator" that was left by the previous input. By doing that, it solved all the runtime Exception issues, hence my ATM application runs perfectly (as it did in the previous jre). Therefore, the pizza ap. and the ATM ap. are running as they should. You and your colleagues (Fred Rosenberger, Knute Snortum, and Henry Wong) taught me a lot. In a way, I was hoping that I could not sort out the ATM ap. so I could do a new post with only the ATM ap. and I could display the use of the right terminology this time so you could understand and solve my problem very quickly. If you or any of your colleagues come to Thailand, let know and we will hoist some beers because I enjoy being around people who are involved with Java. Over here I do not know anyone who does coding so I enjoy the Moose Ranch very much. Thanks!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where abouts in Thailand are you?

BTW the easiest way to use code tags is to highlight the code and then click on the code button. The opening and closing code tags will then be added for you at each end of the highlighted block of text.
 
lee shotwell
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Tony Docherty: I live in Nonthaburi with my family in a guarded housing estate. Nonthaburi is about two hours north of Bangkok. We live here because I used to teach at the Roman Catholic school across the road. I no longer teach there, but me and my family like the area so we stayed on. I have lived in Thailand for 16 years and I was a teacher for 12 of those years. Before that I owned a company in the U.A.E. for 7 years. I married a Thai girl 15 years ago and now we have 2 sons and a baby daughter who is now 2 years old. I have not worked for 4 years. I study Java coding because it really turns me on. It has not been easy for me because my background is in the social sciences and I study on my own. I took an online Java class 6 years ago at Foothill J.C. The instructor said that you did not need any prior knowledge of computing, ha, ha. I had to withdraw because I did not understand what I was doing, but I worked and worked on those class assignments on my own and also took an Algebra course. I think I will take that class again in the Spring of next year. Presently, I study online with Oracle. If you visit Thailand, make sure you e-mail me and we can meet in Bangkok and hoist a few beers. By the way, when I was a student I lived in Palo Alto. At 11PM a little pub off University Av. would sell mugs of beer for 5 cents for half an hour. You can be sure that every one dropped their studies and got down there, but often, after that half hour, we did not make it back to our studies. So, for sure, let me know when you are coming. I see you already know some Thai. I speak it fluently, but I do not read or write it. Cheers!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lee, its probably best if we continue this discussion via PM so we don't fill the thread with personal stuff.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!