-
September 19th, 2012, 02:07 PM
#1
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
-
September 19th, 2012, 06:45 PM
#2
Re: Exception in thread "main" java.lang.NullPointerException problem.......
-
September 20th, 2012, 03:20 PM
#3
Re: Exception in thread "main" java.lang.NullPointerException problem.......
Please use code tags when posting code and also post the full error message and stack trace as it tells us exactly what and where the error is.
BTW prompting for a reply after waiting only 4 hours is not going to help your question get answered - we all help out here in our spare time so I'm afraid you just have to wait until someone has time to answer your post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|