This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Need help with this code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help with this code" Watch "Need help with this code" New topic
Author

Need help with this code

Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
Hiya, I'm writing a java application which renders a pie chart based on input. So far I havn't added the rendering of the actual pie chart, but I keep getting a really annoying runtime error when I try to test the data (i.e. click OK). I'm relatively new to java coding so I don't understand what is wrong.

Here is the code:



pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Hello and welcome to the Ranch!

So, what error message are you seeing, and what line of code causes the error? The more information you can give us, the better we'll be able to help you.

Much luck and again, welcome!
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
The error message I get is:

C:\JavaWork>java PieChartMainMenu
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException:
1 >= 1
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.JTabbedPane.getComponentAt(Unknown Source)
at PieChartMainMenu.returnDataPanel(PieChartMainMenu.java:55)
at PieChartMainMenu$PieGraphicPanel.paintComponent(PieChartMainMenu.java
:285)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

This happens when I press the okButton with one data tab. I don't really understand why I get an ArrayIndexOutOfBoundsException.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Why are you using Vector, rather than one of the more modern implementations of List? Is it that it is required by one of the Components? I know there are things like Combo-boxes which still require Vector.
You are trying to gain access to the second (=index 1) member of a 1-member array or list.
Or: to put it another way: You have one element in the Vector and you are trying to access its second element.
Remember the first member of a List or array is no 0.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
In your code:


You're getting components by using getComponents() and then using an array index to get the components. This is a very fragile way of getting a reference to an object. Why not use the variable that you already have in your class? As it is, it is likely that components[1] doesn't exist ([0] is probably the contentPane).
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Campbell Ritchie wrote:Why are you using Vector, rather than one of the more modern implementations of List? Is it that it is required by one of the Components? I know there are things like Combo-boxes which still require Vector.
You are trying to gain access to the second (=index 1) member of a 1-member array or list.
Or: to put it another way: You have one element in the Vector and you are trying to access its second element.
Remember the first member of a List or array is no 0.


I think that he's not explicitly using Vector but rather is trying to get a reference to a component from a Container which likely uses a Vector to hold all of its components.
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
pete stein wrote:In your code:


You're getting components by using getComponents() and then using an array index to get the components. This is a very fragile way of getting a reference to an object. Why not use the variable that you already have in your class? As it is, it is likely that components[1] doesn't exist ([0] is probably the contentPane).


Which variable do you mean...?

Also, I'm not intentionally using vectors, where does that come?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Dss Ss wrote:
Which variable do you mean...?


You're trying to get the JTabbedPane. How many JTabbedPane variables does your program have? On a side note, why are any of the variables static?


Also, I'm not intentionally using vectors, where does that come?

I think that Containers hold their Components in an internal Vector. The JFrame holds only one Component, the contentPane, so in the components array it is held in position [0]. You're trying to get componets[1] which simply doesn't exist. But even if it did, this is not a good way to get your data. Rather your data should be accessible via a class getter method.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

The JFrame holds only one Component, the contentPane

I guess you meant to say root pane

The JFrame holds the rootPane, which holds the glassPane and layeredPane, the latter of which holds the contentPane and menu (if any).


luck, db
There are no new questions, but there may be new answers.
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
The JFrame holds only one Component, the contentPane, so in the components array it is held in position [0]. You're trying to get componets[1] which simply doesn't exist. But even if it did, this is not a good way to get your data. Rather your data should be accessible via a class getter method.


So this is the code causing the problem:





In the second line, this. refers to the frame and mainPanel refers to the panel I use to hold the three other panels for the first swing interface. Surely that means there are 3 elements in mainPanel? The tabbed pane is the second component added to the mainPanel so it should be at index [1] and there should be at least one pane in the tabbed pane. How else can I access the panes held in the tabbed pane?

Also, the variables weren't meant to be static, I changed that. I can only see one JTabbedPane in my code.
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
Ok I changed the for loop in line 282 to have a condition of < not <=, but now I get this error message

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at PieChartMainMenu$PieGraphicPanel.paintComponent(PieChartMainMenu.java
:301)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.BufferStrategyPaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknow
n Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Any help?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
Tom Huddersfield wrote: . . .
Also, I'm not intentionally using vectors, where does that come?
I presume Pete Stein's explanation in the post previous to that is correct; there is a Component which hides a Vector in its implementation somewhere, rather than a more efficient and modern implementation (eg ArrayList).
You will find Vector towards the top of the stack trace printed from your exception.
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
How can I fix that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Let's give you a little lesson in how to read exception stack traces. Let's take your first example:
That's the exception that has occurred (java.lang.ArrayIndexOutOfBoundsException), its message (1 >= 1) and the thread it occurred in ("AWT-EventQueue-0", the EDT).
That's the actual piece of code the exception occurred at. With your own code you can usually see the file and line number, but you can usually only see those of the core Java classes when running the JDK's java tool, not the JRE's.
That's the piece of code that was calling the above line.
Again, the piece of code that was calling the above line. This is your code; the file is PieChartMainMenu.java and the line is 55.
Again, the piece of code that was calling the above line.
You usually start at the top, and ignore all the classes you did not write. The first line of your code (line 55) is where you need to start checking. Mostly, if that's not the top line, you provided an incorrect method argument. It's possible there is a bug in the library you are using (in this case the core API) but usually that's not the case.

Now your second example:
OK, a NullPointerException. No message is given.
And there's your class at the top. Line 301 is exactly where you need to check. Somewhere you have something at the left hand side of a . that is null. In this case, I suspect "theDatas[i].size.getText()". You have an array of size 3 of which you only set as many elements as you have tabs. If you only have 0 or 1 tabs then you will have 2 or 1 null elements (you're using <= for the number of tabs). As such, for i = 1 and/or i = 2, theDatas[i] is null.


Oh, and you definitely want to call super.paintComponent(g) as the first statement of your paintComponent method - which should remain protected, not public.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tom Huddersfield
Greenhorn

Joined: Dec 19, 2010
Posts: 7
That was really helpful, I managed to fix the problem now
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

You're welcome
 
 
subject: Need help with this code
 
Similar Threads
Just finished my first GUI program (a planner application) ... could you give me feedback?
could someone look at my code and help me with a JScrollPane in a JTextPane please?
completely lost in terms of trying to understand Collections.synchronizedList
how to align components in GridBagLayout in JAVA swing
JTextArea problems in GridbagLayout