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
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! :P
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.
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?
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
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.