Results 1 to 13 of 13
- 06-03-2011, 08:05 PM #1
Member
- Join Date
- Jun 2011
- Location
- Charleston, SC
- Posts
- 4
- Rep Power
- 0
Me and Null Pointer Exception are having issues...
So I am a bit over my head, been learning Java for 6 days now and have written my first code longer than 30 lines (actually I am somewhere around 300 lines right now with console only input and output, also only about 80% done the coding that will make the program do it's job, still have to clean it, add error codes and error prevention, add code for unexpected entries, add a graphical interface... It is something I will keep working on as I learn more)
Background info:
I used to do some very light basic programming in 1982-1986, took 1 basic class and 1 cobol class in college in 1997/98, also spent about 2 years hand coding html in 1996-98.
General Java questions:
my problem with java so far has been it frequently calls my variables by types other than what they are, from web/forum searches I am learning the problem is caused by certain commands I am using force specific variable types, something I can avoid (mostly) if I understood one thing:
is memory pre-allocated for a variable based off of its type?
example would be the "byte" is enough bits reserved for the maximum storage space a byte could use, allocated during the application run, or only the space that is actually used when it is assigned a value?
What I am getting at above is why would you use the int type in a program that only requires 1 or 2 digit numbers? it obviously takes up more memory space than a byte would.
-------------------------------------------------------------
now on to why I am actually posting this here:
I am using Eclipse, and I received a java.lang.NullPointerException error, when I clicked on the part of the error showing the line number it took me to a commented line:
//assign user input to variable
obviously not a Null Pointer issue, the two lines above are:
//prompt the user to enter if they want to drop the lowest
out.print("Do you want me to drop the lowest? (y or n) ");
and the two lines below are:
yOrN = userInput.findInLine(".").charAt(0);
out.println();
the only thing I could think of was the variable yOrN was causing the issue, so I added teh following line about 15 or so lines up in the code:
//attempting to resolve a null pointer error
yOrN = 'n';
recompiled and received the same error.
the variable yOrN was initialized as type "char"
There are other parts of my code where I received some weird responses from the compiler, but I was able to do things that to me made no sense, but made the error go away and the new code appears to do same thing, just with more code used to do it.
I'll post the entire code block I have, if needed, but I don't really want to. (not that it is super secret or anything, but I know my coding style sucks still and don't need to put it on the internet for it to be stored for eternity on some web archive database.)Last edited by finndo; 06-03-2011 at 08:15 PM. Reason: added second line above and below to error section
- 06-03-2011, 08:14 PM #2
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 08:32 PM #3
You need to copy and paste the full text of error messages here.received some weird responses from the compiler,
- 06-03-2011, 08:44 PM #4
finndo,Last edited by finndo; Today at 07:15 PM. Reason: added second line above and below to error section
Don't keep on modifying your original post. Instead, reply to the thread and provide the information. Modifying the first post doesn't notify us about the changes made.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 06-03-2011, 09:41 PM #5
Member
- Join Date
- Jun 2011
- Location
- Charleston, SC
- Posts
- 4
- Rep Power
- 0
I only modified the post because no one had responded yet and didn't want to double post...
Java Code:public static void main(String[] args) { //create a variable to store the user input Scanner userInput = new Scanner(System.in); //create a variable to store our random number Random dieRoll = new Random(); //random variable for storing a single letter user answer to a question char yOrN;
@Norm - they were mostly error stating it couldn't stuff an int into a boolean while I was trying to do an if statement with comparisons, but I am not using booleans, so instead I nested some if's to create the same end result, as I learn more about java I may figure it out. It will be a good 2 months or more before I am at a point to complete this little project anyway.
- 06-03-2011, 09:45 PM #6
Is there a question in your last post?
- 06-03-2011, 10:04 PM #7
Member
- Join Date
- Jun 2011
- Location
- Charleston, SC
- Posts
- 4
- Rep Power
- 0
nope, was just providing an explanation for my lack of providing a response to your previous post about posting the error code to my weird compiler errors, Having already made code changes to prevent the error from continuing to occur, I feel a wee bit too lazy to go back, find them, undo the fixes, and re-run the compiler to post the errors. Maybe when I have more experience and am reviewing the code I'll chuckle to myself and then fix it.
As for my current issue (Null Pointer Exception) I tried to resolve it by changing code, and was not successful. As such I posted here for others amusement at my pathetic attempt to learn Java. All the while hoping to get a resolution without providing enough detail... Hindsight tells me to just post the stupid code... All it would take is someone saying "I don't see anything wrong with this code, it actually looks good for your 6th day! Can you please post the rest of it?" and I'd happily do so...
I did hold back the piece of info that I started on this program three days ago... Had to look up a lot of things when they didn't work the way I wanted them to... so it took me a while.
- 06-03-2011, 11:06 PM #8
I wouldn't discount this line as the source of your problem:
The documentation for Scanner.findInLine says:Java Code:yOrN = userInput.findInLine(".").charAt(0);
The API docs are your friend.If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner's position is unchanged.Get in the habit of using standard Java naming conventions!
- 06-03-2011, 11:10 PM #9
Why are students shown how to chain commands together like that, instead of doing one step at a time and testing the results before proceeding to the next step?
I guess they're optimistic that everything will work as planned.
I've seen that as a problem many times.
- 06-03-2011, 11:19 PM #10
Man, I don't know. Why are students told about the garbage collector? Or reflection? Or told to write an applet as their first program?
Get in the habit of using standard Java naming conventions!
- 06-03-2011, 11:43 PM #11
Member
- Join Date
- Jun 2011
- Location
- Charleston, SC
- Posts
- 4
- Rep Power
- 0
if it makes you feel any better, I don't know a thing about the garbage collector, or reflection!
I will agree that that line could very well be the culprit, it is the first findInLine request in the code, and is not something I have used in the past. I'll look into it, any ideas why it would error before the user gets an opportunity to enter anything? I used it to prevent the user from entering more than one letter (ie spelling out the word Yes, or accidentally hitting another key) and disrupting the code below (It goes into a switch with a case for both little 'y' and cap 'Y', breaking and ignoring any other entries.
If I cannot figure it out, would you suggest replacing it with a normal nextString going into a different variable and then pull the first letter out of the input into YorN variable?Java Code://prompt the user to enter if they want to drop the lowest out.print("Do you want me to drop the lowest? (y or n) "); //assign user input to variable yOrN = userInput.findInLine(".").charAt(0); out.println(); //prompt the user to enter how many to drop if they want to drop the lowest switch (yOrN) { case 'Y': case 'y': out.println("How many of the lowest stats rolled do you want me to drop before dislplaying them?"); out.print("(example would be: roll 8 stats and drop the lowest *2*) "); //assign user input to variable lowStat = userInput.nextByte(); out.println(); break; }
- 06-03-2011, 11:59 PM #12
vsJava Code:yOrN = userInput.findInLine(".").charAt(0);
Java Code:String afterDot = userInput.findInLine("."); // What does this do? if((afterDot != null) && (afterDot.length() > 0)) { yOrN = afterDot.charAt(0); }else { // Ask the user for more input or set a default: yOrN = 'n'; // Set a default }Last edited by Norm; 06-04-2011 at 12:05 AM. Reason: findInLine is a Scanner method
- 06-04-2011, 12:00 AM #13
I'm actually not very familiar with the Scanner class. I'm looking at its API docs right now, and not seeing a nextString method. I'm also surprised to not see a nextChar method. Most of the other primitive types are represented.
I'd try something like this:
Java Code:String scanned = null; while(scanned == null) { scanned = userInput.findInLine("."); } yOrN = scanned.charAt(0);Get in the habit of using standard Java naming conventions!
Similar Threads
-
null pointer exception
By bequick01 in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:31 PM -
Null pointer Exception
By peiceonly in forum New To JavaReplies: 8Last Post: 09-05-2010, 06:48 PM -
Help with Null Pointer Exception
By Beginner in forum New To JavaReplies: 2Last Post: 04-17-2010, 04:41 PM -
Null Pointer Exception
By demiser55 in forum New To JavaReplies: 1Last Post: 09-22-2008, 06:33 PM -
null pointer exception
By cityguy503@yahoo.com in forum New To JavaReplies: 4Last Post: 08-22-2008, 07:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks