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&¤tMin<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();
}
}