I have a few questions/concerns with parts of my Java code.
I created the following Vector objects in my class:
private Vector data;
private Vector dataLabels;
private Vector dataColors;
If I want to convert these vectors to generic (template) type, does that mean that I have to change Vector to Vector<?> ? Is there another way to convert it to generic type?
I am not sure what is wrong with this paint() method:
public void paint(Graphics g)
{
setSize(200,250);
Image duke = Toolkit.getDefaultToolkit(
).getImage
("duke2.gi
f");
g.drawImage(duke, 80, 10, this);
for (int i = 0; i < data.size(); i++)
{
int yposition = 100+i*barWidth;
g.setColor((Color) dataColors.elementAt(i));
int barLength = ((Integer) data.elementAt(i)).intValu
e();
g.fillOval(100, yposition, barLength, barWidth);
g.setColor(Color.black);
g.drawString((String) dataLabels.elementAt(i), 20, yposition+10);
}
}
I keep getting a NullPointerException at the following line:
g.drawImage(duke, 80, 10, this);
I have tried catching it, but it never catches it. How do I catch the exceptions in this method?
I have also attached the gif file that is used in the paint() method.
duke2.gif
Open in new window
As for your NullPointerException, my guess is that the "duke" variable that is returned from the .getImage method is null. If I remember correctly, that method returns null in a lot of cases if it can't load the file. You should check for a null value before you use it.As for why you can't catch the exception, I would need to see the code of what you tried to give any advice.