Solved

Converting Objects to Generics and Handling Exceptions

Posted on 2014-04-26
3
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 36

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

690 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