Using the file of sunset data, write a program using graphics that shows the sunset and sunrise. When I run the program, it says java.lang.NullPointerException. I want to know where I put the sunset data file. I am using eclipse.

Here is my program:

import javax.swing.*;
import java.io.*;
import java.util.*;
import java.awt.*;

//panel to be embedded on the frame for painting
class SunSetRise extends JPanel
{
FileInputStream fis;
BufferedReader br;
int hr1,hr2,min1,min2,totalseconds,durationofday;
String it=null;
Calendar date;
int currentMonth, currentDay,currentHour=0,currentMin=0;
int day,flag=0;
int month=1;
int start=-101;
StringTokenizer st=null;
MySetRise msr;

SunSetRise()
{
   try
   {
   //file to be read for day and its duration
   fis=new FileInputStream("sundata.txt");
   br=new BufferedReader(new InputStreamReader(fis));
   }
   catch(Exception e)
   {
   }
   setLayout(new FlowLayout());
   setOpaque(true);
   //get calendar object
   date=Calendar.getInstance();
   //read current day and month
   currentMonth=date.get(Calendar.MONTH) + 1;
   currentDay=date.get(Calendar.DAY_OF_MONTH);
   msr=new MySetRise();
}

//this class does all the calculation and renderers simulated sun to panel by calling repaint() 
class MySetRise implements Runnable
{
Thread t;

MySetRise()
{
t=new Thread(this);
t.start();
}

public void run()
{
//reading data from file
	try
	{
	String s;
		//keep reading line one by one
		while((s=br.readLine())!=null)
		{
		//form tokens of the string
		st=new StringTokenizer(s," ");
			//read token by token
			while(st.hasMoreTokens())
			{
			//first token is day
			day=Integer.parseInt(st.nextToken());
			//second token is sun rise hour
			it=st.nextToken();
			hr1=Integer.parseInt(it);
			//third token is sun rise minute
			it=st.nextToken();
			min1=Integer.parseInt(it);
			//fourth token is sun set hour
			it=st.nextToken();
			hr2=Integer.parseInt(it);
			//fifth token is sun set minute
			it=st.nextToken();
			min2=Integer.parseInt(it);
				//increment the month of file if next day = 1 is found
				if((day==1) && (flag==1))
				{
				month++;
				}
				//check if match found in file
				if(currentMonth == month && currentDay == day)
				{
				     //read current system hour and minute
				     currentHour=date.get(Calendar.HOUR_OF_DAY);
   				     currentMin=date.get(Calendar.MINUTE);
				     System.out.println("Current Date "+currentMonth+"/"+currentDay);
				     System.out.println("Current Time "+currentHour+":"+currentMin);
				     if(currentHour<=hr1&&currentMin<min1)
				     {
					//the system time is less than the sun rise time wait for
					//much time
					try
					{
					Thread.sleep(((hr1-currentHour)*60*60+(min2-currentMin)*60)*1000);
					}
					catch(Exception ae)
					{
					System.out.println(ae.getMessage());
					}
				     }
				     else if(currentHour<hr2)
				     {
					//the system time greater than equal to sun rise time
					//and less than the sun set time then calculate the start
					//position of sun
					durationofday = ( (hr2-hr1)*60 + (min2-min1) )*60;
				   	totalseconds = (hr2 - (currentHour+1))*60*60 + (60 - currentMin + min2)*60;
					System.out.println("Seconds rem : "+totalseconds);
					start = (1000*totalseconds)/durationofday;
				     }
				     else if(currentHour==hr2)
				     {	
					durationofday = ( (hr2-hr1)*60 + (min2-min1) )*60;
					if(currentMin<min2)
					{
						totalseconds = (currentMin - min2)*60;
						start = (1000*totalseconds)/durationofday;
					} 
				     }
				     else
				     {
					//the sun is already set
					start=-101;
				     }
				  
				while( start >= (-100) )
				{
				//simulate the rising and setting of sun
					try
					{
					Thread.sleep(durationofday);
					}
					catch(Exception ae)
					{
					System.out.println(ae.getMessage());
					}
					//System.out.println("Start = "+start);
					start--;
					repaint();
				}
				System.exit(0);
				}
			}
			flag=1;
			st=null;	
		}
	}
	catch(Exception e)
	{
		System.out.println(e);
	}
}
}


protected void paintComponent(Graphics g)
{
//paint method to paint sun
super.paintComponent(g);
setBackground(Color.WHITE);
setForeground(Color.RED);
g.fillOval(start,270,100,100);
}
}

//frame class
class SetRise extends JFrame
{
SetRise()
{
super("Sun rises in the east and sets in the west===== WEST<-<-to-<-<-EAST =====");
setSize(1000,740);
setLayout(new GridLayout(1,1));
add(new SunSetRise());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public static void main(String args[])
{
SetRise sr=new SetRise();
}
}

Recommended Answers

All 3 Replies

try adding
File temp = new File("filename.txt");
System.out.println(temp.getAbsolutePath());

if you create a FileInputStream, your vm will automatically assume the file already exists, while when using a File object, you can first verify and create one if necessary.

are you saying to put this in my class?:
File temp = new File("filename.txt");
System.out.println(temp.getAbsolutePath());

I don't think I have the text file in the right place. The text file is just a bunch of numbers that I have copied from a handout our teacher gave us. What I did was copy these numbers and went to the java project that the SunSetRise class is in and clicked new File. Then I saved it to the src package that my SunSetRise class is in. Does this seem right? The program still isn't working

doesn't matter.
that line will just 'create a File', whether or not there actually is a physical file on your disk present, but that way, you can take a look at the path where your code is expecting you to put your files.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.