Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception in thread "main" java.lang.NullPointerException problem.......

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Exception in thread "main" java.lang.NullPointerException problem.......

    So when I run this code, I get this "Exception in thread "main" java.lang.NullPointerException" error:


    import feiner.bcs345.health.datetime.*;
    import java.util.*;
    import java.io.*;

    public class datetimetest
    {

    /**
    * @param args
    */
    public static void main(String[] args)
    {
    //************************************************** *****************************************
    //MyDate Test
    //************************************************** *****************************************
    Scanner s = null;
    PrintStream ps = null;

    MyDate Date1;
    MyDate Date2;



    // New instance using a default constructor.
    Date1 = new MyDate();

    //Set methods alter the values.
    Date1.SetMonth(2);
    Date1.SetDay(2);
    Date1.SetYear(2001);

    //Print statements output the set values to confirm they are correct.
    System.out.print("Month = ");
    System.out.println(Date1.GetMonth());

    System.out.print("Day = ");
    System.out.println(Date1.GetDay());

    System.out.print("Year = ");
    System.out.println(Date1.GetYear());
    System.out.print("\n");



    //New instance using a constructor with parameters.
    Date1 = new MyDate(3, 3, 2002);

    //Print statements output the set values to confirm they are correct.
    System.out.print("Month = ");
    System.out.println(Date1.GetMonth());

    System.out.print("Day = ");
    System.out.println(Date1.GetDay());

    System.out.print("Year = ");
    System.out.println(Date1.GetYear());
    System.out.print("\n");



    //New instance using a constructor with parameters.
    Date1 = new MyDate(4, 4, 2003);

    //Read method reads values set by constructor.
    Date1.Read(s);

    //Write method prints values.
    Date1.Write(ps);


    }

    }





    And Here is the class im using with it:

    package feiner.bcs345.health.datetime;
    import java.util.*;
    import java.io.*;

    public class MyDate
    {
    //Attributes
    private int Month;
    private int Day;
    private int Year;

    //Behaviors
    public int GetMonth() {return Month;}
    public int GetDay() {return Day;}
    public int GetYear() {return Year;}

    public void SetMonth(int m) {Month = m;}
    public void SetDay(int d) {Day = d;}
    public void SetYear(int y) {Year = y;}

    public void Write(PrintStream ps)
    {
    ps.printf("%d\t%d\t%d\t, Month, Day, Year");
    }

    public void Read(Scanner s)
    {
    Month = s.nextInt();
    Day = s.nextInt();
    Year = s.nextInt();
    }

    // Default Constructor
    public MyDate()
    {
    Month = 1;
    Day = 1;
    Year = 2000;
    }

    // Constructor
    public MyDate(int m, int d, int y)
    {
    Month = m;
    Day = d;
    Year = y;
    }
    }





    Can someone please help me make the "Read" and "Write" methods work correctly in main? I think it has something to do with initializing the scanner and/or printstream or something i really don't know i'm a beginner. Thanks in advance


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    26
    My Mood
    Bored
    Thanks
    6
    Thanked 5 Times in 4 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException problem.......

    Hello!
    Please wrap your code using the tags [code=java] and ending the code with [ /code].
    Is that the full error message that gets print out? It doesn't tell you which line the null value is on?

    EDIT: Haha Norm I beat you to it!

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException problem.......

    the entire error is:

    Exception in thread "main" java.lang.NullPointerException
    at feiner.bcs345.health.datetime.MyDate.Read(MyDate.j ava:36)
    at datetimetest.main(datetimetest.java:73)

    the lines are:
    Month = s.nextInt(); -in the Read method

    and

    Date1.Read(s) in main.
    Last edited by Adam802; September 19th, 2012 at 10:41 PM.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException problem.......

    Month = s.nextInt(); in the Read method is returning null i think when i call it in Date1.Read(s) in main.

    Should I not make Scanner s and PrintSteam ps null? What should I make them instead?

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException problem.......

    Hello Adam802!

    When you get a NPE you should go to the line the compiler tells you, print the variables in that line and find out which of them is null. Then you can backtrack to your code and find out why it doesn't have a valid value (null).

    Quote Originally Posted by Adam802 View Post
    Month = s.nextInt(); in the Read method is returning null i think when i call it in Date1.Read(s) in main.
    Should I not make Scanner s and PrintSteam ps null? What should I make them instead?
    Yes you shouldn't. Because calling a method of an object that is null (ie it doesn't already exist) doesn't make sense. You should create a new Scanner (and PrintStream) object before trying to use them. Take a look at here on how to create a new object.

    Hope this helps.

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException
    By Azz289 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2012, 01:44 PM
  2. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By manzili in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 9th, 2011, 12:02 PM
  4. Replies: 7
    Last Post: May 30th, 2011, 09:11 AM
  5. Exception in thread "main" java.lang.NullPointerException
    By Tsark in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 11th, 2011, 08:39 AM