Null Pointer Exception.......

Thread Solved
Reply

Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Null Pointer Exception.......

 
0
  #1
Jun 3rd, 2008
I'm implementing a Timer for different cities using GregorianCalendar class of java.I got a Null Pointer Exception in the following code in ---- target.timeElapsed(this) line.After a lot of practices,I don't got it out.Plzz help me to sort it out..


Here's my code----------



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
TimerTestFrame frame = new TimerTestFrame();
frame.setVisible(true);
}
}
class TimerTestFrame extends JFrame
{
public TimerTestFrame()
{
setSize(450,300);
setTitle("TimerTest");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
contentPane.setLayout(new GridLayout(2,3));
contentPane.add(new ClockCanvas("San Jose","GMT-8"));
contentPane.add(new ClockCanvas("Taipei","GMT+8"));
contentPane.add(new ClockCanvas("Berlin","GMT+1"));
contentPane.add(new ClockCanvas("New York","GMT-5"));
contentPane.add(new ClockCanvas("Cairo","GMT + 2"));
contentPane.add(new ClockCanvas("Bombay","GMT+5"));
}
}
//implementing a TimeListener interface........


interface TimeListener
{
public void timeElapsed(Timer t);
}

//Timer class which runs in it's seperate Thread..

class Timer extends Thread
{
TimeListener target;
private int interval;
public Timer(int i,TimeListener t)
{
target = t;
interval = i;
setDaemon(true);
}
@Override
public void run()
{
try
{
while(!(interrupted()))
{
sleep(interval);
target.timeElapsed(this);//This line is giving the corresponding error..
}
}
catch(InterruptedException e)
{
}

}
}

//ClockCanvas class having interface TimeListener........

class ClockCanvas extends JPanel implements TimeListener
{
private int seconds =0;
private String city;
private int offset;
private GregorianCalendar calendar;
private final int LOCAL = 16;
TimeListener target;
public ClockCanvas(String c,String tz)
{
city = c;
calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
Timer t = new Timer(1000,this.target);
t.start();
setSize(125,125);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawOval(0,0,100,100);
double hourAngle = 2 * Math.PI *(seconds - 3 * 60 * 60)/(12 * 60 * 60);
double minuteAngle = 2 * Math.PI*(seconds - 15 * 60)/(60 * 60);
double secondAngle = 2 * Math.PI*(seconds - 15 )/60;
g.drawLine(50,50,50+(int)(30*Math.cos(hourAngle)),50 + (int)(30*Math.sin(hourAngle)));
g.drawLine(50,50,50+(int)(40*Math.cos(minuteAngle)),50 +(int)(40*Math.sin(minuteAngle)));
g.drawLine(50,50,50+(int)(45*Math.cos(secondAngle)),50 +(int)(45*Math.sin(secondAngle)));
g.drawString(city,0,115);

}
public void timeElapsed(Timer t)
{
calendar.setTime(new Date());
seconds = calendar.get(Calendar.HOUR)*60*60 + calendar.get(Calendar.MINUTE)*60 + calendar.get(Calendar.SECOND);
repaint();
}
}



Plzzzzzzz look out through it........ Thanks in advance..
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,525
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Null Pointer Exception.......

 
0
  #2
Jun 3rd, 2008
  1. public ClockCanvas(String c,String tz)
  2. {
  3. city = c;
  4. calendar = new GregorianCalendar(TimeZone.getTimeZone(tz));
  5. Timer t = new Timer(1000,this.target);
  6. t.start();
  7. setSize(125,125);
  8. }
I believe that in the above code the target: this.target is null;

Your NullPointerException: target.timeElapsed(this); was because target was null. I have search from where it takes value and found the code that I present in the beginning of the post. The ClockCanvas(String c,String tz) is the constructor. Above that constructor you declare: TimeListener target; So when you call for example:
  1. contentPane.add(new ClockCanvas("San Jose","GMT-8"));
the TimeListener target member of the ClockCanvas doesn't take value. So in this:
  1. Timer t = new Timer(1000,this.target);
this.target is null. Then you call the start method and you get a NullPointerException.

Next Time try to put your code in a try-catch and do a printstackTrace(). That way you will be able to see not only where the exception was but also the path it followed before it is thrown. If you have done that you would have seen all the calls that took place and you would have spotted the place where the null target takes value (or doesn't)
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Null Pointer Exception.......

 
0
  #3
Jun 3rd, 2008
thanx a lot....
I got why it is giving Null Point Exception but couldn't get the way to get overrid of it as I'm a newbie to java.Plzzzzz can u figure out the way so that the problem can be overtaken.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,525
Reputation: javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light javaAddict is a glorious beacon of light 
Solved Threads: 209
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Null Pointer Exception.......

 
0
  #4
Jun 3rd, 2008
Instantiate the TimeListener target; in the ClockCanvas constructor before you use it.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 14
Reputation: funtoosh is an unknown quantity at this point 
Solved Threads: 0
funtoosh funtoosh is offline Offline
Newbie Poster

Re: Null Pointer Exception.......

 
0
  #5
Jun 3rd, 2008
thanx a lot man!! u rock!!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC