Solved

Converting Objects to Generics and Handling Exceptions

Posted on 2014-04-26
3
327 Views
Last Modified: 2014-04-27
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
0
Comment
Question by:AttilaB
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 300 total points
ID: 40025373
If I want to convert these vectors to generic (template) type, does that mean that I have to change Vector to Vector<?> ?
You would really call that "using generics". Yes, it may eliminate some compiler errors, but it is still a vector that can store any type of object, just like before. Using generics would mean something more like this...
private Vector<Integer> data;
private Vector<String> dataLabels;
private Vector<Color> dataColors;

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.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 200 total points
ID: 40025491
Image duke = Toolkit.getDefaultToolkit().getImage("duke2.gif");

Open in new window


That's a somewhat outdated way of loading images and it can lead to problems through its being done asynchronously. javax.imageio,ImageIO.read will stand you in better stead. It's also in the wrong place: load resources outside paint, preferably in the ctor or better still a dedicated method after the ctor has been called.

 Also as mccarl says, make sure that the image is where you think it is in relation to the current directory.
http://technojeeves.com/index.php/aliasjava1/91-find-the-current-directory-in-java
0
 

Author Closing Comment

by:AttilaB
ID: 40026545
Thank you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not needed 13 92
Re-position the objects 7 95
session migration servlets 2 24
Q2. How to run DAX query from SSMS. I don’t see any DAX query type. 1 43
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now