Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

java.lang.nullPointerExecption?

Options
  • 28-10-2013 1:29pm
    #1
    Registered Users Posts: 474 ✭✭


    Hi there,
    I am in the process of typing out an example from the OCA Java SE 7 Programmer I Study Guide and I keep encountering this error on this one example(Seriously I HATE this book! It is full of errors, misprints, shoddy code and is written horribly)

    Anyway here is the code:
    package com.ocaj.exam.tutorial;//Package statement
    import java.util.ArrayList; //import class Arraylist from the java.util package
    import java.io.*; //imports all classes from the java.io package

    public class MainClass {
    public static void main(String[] args) {
    Console console = System.console(); //creates console from java.io package
    String planet = console.readLine("\nEnter your favourite planet: ");
    ArrayList planetList = new ArrayList(); //creates a list of planets
    planetList.add(planet);//adds userinput to list
    planetList.add("Gliese 581 c"); //adds a string to the list
    System.out.println("\n Two cool planets: " + planetList);
    }

    }

    The output is supposed to be:
    Enter your Favourite Planet: Jupiter
    Two cool planets: [Jupiter, Gliese 581 c

    However I keep getting the java.lang.nullPointerException at line 8, which is:
    String planet = console.readLine("\nEnter your favourite planet: ");

    Can anyone help me out? I have looked over the example in the book multiple times and I don't know what is wrong.


Comments

  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    I assume you are trying to run it in Eclipse. Run it in a windows command console and the console will not be null.

    To do this, make sure your environment variable is set up. This will allow you to use the java compiler more easily in the windows command prompt console.

    Start > Control Panel > System > Advanced System Properties > Environment Variables

    In "User variables" select "PATH" if exists, and press "Edit...".


    Add ";" at the end of the existing entry as a seperator, then add the path to your Java binaries folder, e.g. ;C:\Program Files\Java\jdk1.7.0_11\bin

    If it does not exist, press "New...".

    Var Name: PATH
    Var Value: C:\Program Files\Java\jdk1.7.0_11\bin

    Press "OK".

    Open command prompt. Change directory to your project folder e.g. "cd C:\Users\Umekichi\Documents\Java\"

    Compile your class e.g. "javac MainClass.java" (*.java compiles all classes in folder).

    Run the java program, "java MainClass".


  • Registered Users Posts: 474 ✭✭Umekichi


    I assume you are trying to run it in Eclipse. Run it in a windows command console and the console will not be null.

    To do this, make sure your environment variable is set up. This will allow you to use the java compiler more easily in the windows command prompt console.

    Start > Control Panel > System > Advanced System Properties > Environment Variables

    In "User variables" select "PATH" if exists, and press "Edit...".


    Add ";" at the end of the existing entry as a seperator, then add the path to your Java binaries folder, e.g. ;C:\Program Files\Java\jdk1.7.0_11\bin

    If it does not exist, press "New...".

    Var Name: PATH
    Var Value: C:\Program Files\Java\jdk1.7.0_11\bin

    Press "OK".

    Open command prompt. Change directory to your project folder e.g. "cd C:\Users\Umekichi\Documents\Java\"

    Compile your class e.g. "javac MainClass.java" (*.java compiles all classes in folder).

    Run the java program, "java MainClass".


    Excellent thanks!
    Out of curiosity why does it not work with Eclipse?


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Umekichi wrote: »
    Excellent thanks!
    Out of curiosity why does it not work with Eclipse?

    Eclipse bug


  • Registered Users Posts: 474 ✭✭Umekichi


    Just tried it there with CMD and this is what I get:
    Note: MainClass.java uses unchecked or unsafe operations
    Note: Recompile with -Xlint: unchecked for details


  • Registered Users Posts: 345 ✭✭Dr.MickKiller


    I think you need to add the type when creating a new ArrayList.
    ArrayList<String> planetList = new ArrayList<String>();


  • Advertisement
  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Umekichi wrote: »
    Just tried it there with CMD and this is what I get:

    Don't worry about that, it will still run. Use 'java MainClass' to run it.


  • Registered Users Posts: 474 ✭✭Umekichi


    Don't worry about that, it will still run. Use 'java MainClass' to run it.

    Its just throwing me up even more errors now, I'm just gonna leave it and move on. Maybe I'll come back to it later

    Thanks for all the help though guys, yis are awesome :D


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    It worked for me! I copied your code (I just removed the package declaration as I didn't have it in that folder structure), compiled it, ran it. Asked for a planet name, then printed the cool planet list!


  • Registered Users Posts: 474 ✭✭Umekichi


    I just got Exception in thread "main" java.lang.NoClassDefFoundError: Main Class


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    You have complied it? You are trying to compile and run it while being the correct directory?

    Look in the folder to see there is a class called MainClass.class.

    Make sure the class name is "MainClass", no spaces, and the filename is "MainClass.java", i.e. class name must equal filename.

    If that fails, try running it like this: "java -classpath . MainClass".


  • Advertisement
  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Runtime Problems

    Error Messages on Microsoft Windows Systems

    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldApp

    If you receive this error, java cannot find your bytecode file, HelloWorldApp.class.

    One of the places java tries to find your .class file is your current directory. So if your .class file is in C:\java, you should change your current directory to that. To change your directory, type the following command at the prompt and press Enter:

    cd c:\java
    The prompt should change to C:\java>. If you enter dir at the prompt, you should see your .java and .class files. Now enter java HelloWorldApp again.

    If you still have problems, you might have to change your CLASSPATH variable. To see if this is necessary, try clobbering the classpath with the following command.

    set CLASSPATH=
    Now enter java HelloWorldApp again. If the program works now, you'll have to change your CLASSPATH variable. To set this variable, consult the Updating the PATH variable section in the JDK 7 installation instructions. The CLASSPATH variable is set in the same manner.

    Oracle docs


  • Registered Users Posts: 1,109 ✭✭✭Skrynesaver


    Using ther Package name you supplied...
    sykrynesaver@busybox ~/src/java $ ls com/ocaj/exam/tutorial/
    MainClass.class  MainClass.java
    sykrynesaver@busybox ~/src/java $ java com.ocaj.exam.tutorial.MainClass 
    
    Enter your favourite planet: Earth
    
     Two cool planets: [Earth, Gliese 581 c]
    sykrynesaver@busybox ~/src/java $
    


  • Registered Users Posts: 3,078 ✭✭✭onemorechance


    Umekichi wrote: »
    I just got Exception in thread "main" java.lang.NoClassDefFoundError: Main Class
    Using ther Package name you supplied...
    sykrynesaver@busybox ~/src/java $ ls com/ocaj/exam/tutorial/
    MainClass.class  MainClass.java
    sykrynesaver@busybox ~/src/java $ java com.ocaj.exam.tutorial.MainClass 
    
    Enter your favourite planet: Earth
    
     Two cool planets: [Earth, Gliese 581 c]
    sykrynesaver@busybox ~/src/java $
    

    The last error from the OP seemed liked it was just a typo, the space in class name, Main Class.


  • Registered Users Posts: 474 ✭✭Umekichi


    It suddenly started working just there :D
    Don't know why as I didn't do anything different (that I know of xD)


Advertisement