Solved

Converting Objects to Generics and Handling Exceptions

Posted on 2014-04-26
3
328 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 29
check java version using powershell 13 80
Problem to App 4 78
Using VBscript & RegEx, how can I match lines starting with Oracle word 17 57
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

12 Experts available now in Live!

Get 1:1 Help Now