Results 1 to 13 of 13
  1. #1
    finndo is offline Member
    Join Date
    Jun 2011
    Location
    Charleston, SC
    Posts
    4
    Rep Power
    0

    Default 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

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Quote Originally Posted by finndo View Post
    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.
    Which means yOrN isn't the culprit.

    What's the deal with userInput? Can you show some more code? Specifically the one that feeds the userInput value?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    received some weird responses from the compiler,
    You need to copy and paste the full text of error messages here.

  4. #4
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Cool

    Last edited by finndo; Today at 07:15 PM. Reason: added second line above and below to error section
    finndo,

    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.

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  5. #5
    finndo is offline Member
    Join Date
    Jun 2011
    Location
    Charleston, SC
    Posts
    4
    Rep Power
    0

    Default

    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.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Is there a question in your last post?

  7. #7
    finndo is offline Member
    Join Date
    Jun 2011
    Location
    Charleston, SC
    Posts
    4
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    Is there a question in your last post?
    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.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    I wouldn't discount this line as the source of your problem:

    Java Code:
    yOrN = userInput.findInLine(".").charAt(0);
    The documentation for Scanner.findInLine says:

    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.
    The API docs are your friend.
    Get in the habit of using standard Java naming conventions!

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    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.

  10. #10
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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!

  11. #11
    finndo is offline Member
    Join Date
    Jun 2011
    Location
    Charleston, SC
    Posts
    4
    Rep Power
    0

    Default

    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.

    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;
    		}
    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?

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Java Code:
    yOrN = userInput.findInLine(".").charAt(0);
    vs
    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

  13. #13
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default

    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

  1. null pointer exception
    By bequick01 in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 08:31 PM
  2. Null pointer Exception
    By peiceonly in forum New To Java
    Replies: 8
    Last Post: 09-05-2010, 06:48 PM
  3. Help with Null Pointer Exception
    By Beginner in forum New To Java
    Replies: 2
    Last Post: 04-17-2010, 04:41 PM
  4. Null Pointer Exception
    By demiser55 in forum New To Java
    Replies: 1
    Last Post: 09-22-2008, 06:33 PM
  5. null pointer exception
    By cityguy503@yahoo.com in forum New To Java
    Replies: 4
    Last Post: 08-22-2008, 07:22 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •