Link to home
Start Free TrialLog in
Avatar of AttilaB
AttilaB

asked on

Converting Objects to Generics and Handling Exceptions

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.gif");
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)).intValue();
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
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of AttilaB
AttilaB

ASKER

Thank you.