This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting null pointer exception

 
Aayush Patel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can any body help me.i m writing code below..I m getting null pointer exception..

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
//<applet code="proj2" width=500 height=500></applet>
public class proj2 extends Applet implements TextListener//FocusListener
{
TextField tf,tf1,tf2;
Label l;
String st=null,st1=null,st2=null;
int i=0,i1=0,i2=0;
int im[]={0,0,0};
Graphics g;
Color chartColors[]={Color.red,Color.yellow,Color.blue,Color.black,Color.magenta,Color.cyan};
public void init()
{
tf=new TextField();
tf1=new TextField();
tf2=new TextField();
l=new Label();
add(tf);
add(tf1);
add(tf2);
add(l);
setLayout(null);
tf.setBounds(10,15,40,15);
tf1.setBounds(10,40,40,15);
tf2.setBounds(10,65,40,15);
l.setBounds(10,90,40,15);
tf.addTextListener(this);
tf1.addTextListener(this);
tf2.addTextListener(this);
}
/*public void focusGained(FocusEvent te)
{
}*/

public void textValueChanged(TextEvent ae)
{

try{
if(ae.getSource().equals(tf))
{
st=tf.getText();

//l.setText(i+"");
}
if(ae.getSource().equals(tf1))
{
st1=tf1.getText();

//System.out.println(""+i1);
}
if(ae.getSource().equals(tf2))
{
st2=tf2.getText();

}
i=Integer.parseInt(st);
i1=Integer.parseInt(st1);
i2=Integer.parseInt(st2);
im[0]=i;
im[1]=i1;
im[2]=i2;
}catch(NumberFormatException ne){}

update(g);}
public void update(Graphics g)
{
paint(g);
}
public void paint(Graphics g)
{

im[0]=i;
im[1]=i1;
im[2]=i2;
int n=0;
int nn=0;
int angles[] = new int[im.length];
for(i=0;i<im.length;i++)
{n=n+im[i];
l.setText(""+n);
try{//System.out.print("Paint");
if(n!=0)
{angles[i] = (int)((im[i] / n)* 360.0);
g.setColor( chartColors[i] );
int start = 0;
for( int j = 0; j < i; j++ )
{System.out.print("Paint123");
start += angles[i];
}
g.fillArc( 50,50 ,100,100, start, angles[i] );}
}catch(Exception ae){System.out.print(ae);}
}
}





}
 
d kiruthika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Your code seems to be fine. Your code perfectly works in my machine. In the 3 three textboxes, when the focus is changed from any of the text boxes, the sum of the three values is printed in the bottom
 
Aayush Patel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey i know my code is fine.and i also get sum of the number but my problem is that i m not able get piechart which i want to draw..it gives me null pointer exception..so help me to remove that excepation for getting required result and i think the code must have to do work fine..still getting confusion so help me...
 
d kiruthika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am sorry. There is a problem. Change your try segment as follows
try{
if(ae.getSource().equals(tf))
{
st=tf.getText();
i=Integer.parseInt(st);
im[0]=i;
//l.setText(i+"");
}
if(ae.getSource().equals(tf1))
{
st1=tf1.getText();
i1=Integer.parseInt(st1);
im[1]=i1;
//System.out.println(""+i1);
}
if(ae.getSource().equals(tf2))
{
st2=tf2.getText();
i2=Integer.parseInt(st2);
im[2]=i2;
}
This eliminates occurence of NumberFormatException and NullPointerException occuring when the text value is changed everytime.
Similarly make necessary changes in your paint() method. Because after making the change in your try segment and commenting update(g), there is literally no error. So, make changes in the paint() also.
regards,
Kiruthika
 
Aayush Patel
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey dear still same problem going on..U can try on your PC and i want PieChart according to value ...but getting null pointer exeception instead of that......logically code seems to be ok
 
d kiruthika
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Two things were found
1) within your paint() method
(a) alongwith other declarations, have
int counter = 0;
(b) put if condition
if (counter < i)
{
g.setColor( chartColors[i] );
}
(c) increment counter before you leave the for loop in the paint method
counter++;

This eliminates one NullPointerException
2) Even with hardcoded value, when the text value is changed,
NullPointerException arised at this point.
I give up at this point. No more time si there to spend.
Hope it helps.
Kiruthika
g.fillArc( 250,250 ,100,100, 10,90);//start, angles[i] );
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic