Solved

Converting Objects to Generics and Handling Exceptions

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Selenium docs api java index 3 67
Need Help! Getting a syntax error and don't understand why 3 40
java mysql insert application 14 45
Visual Studio hangs on running project 6 61
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

733 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