Solved

Converting Objects to Generics and Handling Exceptions

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

743 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

13 Experts available now in Live!

Get 1:1 Help Now