• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Need help with this code

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:



 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 80781
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Tom Huddersfield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 80781
489
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I fix that?
 
Sheriff
Posts: 22857
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch.
 
Tom Huddersfield
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was really helpful, I managed to fix the problem now
 
Rob Spoor
Sheriff
Posts: 22857
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
reply
    Bookmark Topic Watch Topic
  • New Topic