Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Whats wrong with this code?

  Asked By: Manju    Date: May 09    Category: Java    Views: 843

i am having trouble with my eternal "passing
variables" problem... here is the code I am using... the
entire thing is originally called using the code: f =
new SampleFrame(output); when the data[1] is in
the table, the applet will run, but nothing happens
when the popup window is activated. when it is
replaced with a "text" it works fine... what is wrong with
this code? and here is the rest of the
code: class SampleFrame extends Frame{ private JPanel
topPanel; private JTable table; private JScrollPane
scrollPane; String[] data = new String [5];
SampleFrame(String title) { super(title); MyWindowAdapter
adapter = new MyWindowAdapter(this);
addWindowListener(adapter); Main main = new Main(); data[1] =
"stuff"; main.setData(data); main.show();
} } class Main extends JFrame { private String[]
data; public Main() { super("treeCounter --
current"); String[][] tabledata = { { "text", data[1],
"text"}, { "text", "text", "text" }, { "text", "text",
"text" }, }; String columnheaders[] = { "Head",
"Head", "Head" }; JTable table = new
JTable(tabledata, columnheaders);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
JScrollPane scrollPane =
new JScrollPane(table);
getContentPane().add(scrollPane); addWindowListener(new WindowAdapter()
public void windowClosing(WindowEvent we) {
} }); pack(); } public void
setData(String[] data) { this.data = data; }
} class MyWindowAdapter extends WindowAdapter {
SampleFrame sampleFrame; public
MyWindowAdapter(SampleFrame sampleFrame) { this.sampleFrame =
sampleFrame; } public void windowClosing(WindowEvent we)
{ sampleFrame.setVisible(false);



7 Answers Found

Answer #1    Answered By: Jaspreet Kapoor     Answered On: May 09

The main problem  I see is that your tableData
uses the data  array prior to data[1] being set. The
Main() constructor will be executed prior to the
setData() method. Are you seeing any exceptions thrown? It
would seem that you should get an NullPointerException
since the data[] array is not
initialized.<br><br>Another thing: it is not clear from your message whether
you know or not that Java arrays are indexed starting
from 0. Just something to keep in

Answer #2    Answered By: Alexis Castillo     Answered On: May 09

yes, the data  is set, and yes, the array index is valid, but that is seemingly
not the problem..

Answer #3    Answered By: Dot net Sachin     Answered On: May 09

In the code  you posted that should not be the
case. As I explained before, when you create the
tabledata array in the Main constructor the value for
data[1] is null. Your code is most likely throwing a
NullPointerException. I ran your code as an application and that is
what I received. If you are building this as an
applet, then you should check the applet  console to see
if the error is indeed

Answer #4    Answered By: Yvonne Watkins     Answered On: May 09

i looked up NullPointerException in the java api, but i have no better idea what
it is, or how to fix it...

Answer #5    Answered By: Yvette Griffin     Answered On: May 09

i did some more testing... and i figured out that
perhaps the window  opening is happening before the
variable is being transfered.... do you see how this is
happening? and perhaps how it could be

Answer #6    Answered By: Brent Brown     Answered On: May 09

Well, the easiest way to fix it would be to move
all of the code  in the Main() constructor into a
method called  init() and then call the init() method
after you call setData() and before show().<br><br>Mind
you, that is the easy fix...it is not a very good
design though. In the long run  you should consider
writing your own TableModel implementation for use in
your JTable. It will give you a lot more control over
your table  data.

Answer #7    Answered By: Kay Rodriguez     Answered On: May 09

FWIW, a NullPointerException occurs when you try
to use a variable you have declared but which has no
value. In some cases the compiler can catch these
things, but in others (such as when the variable is an
instance variable) it is a runtime error.<br><br>Get used
to NullPointerExceptions (also known as NPEs). You
will probably see a lot of them if you work with

Didn't find what you were looking for? Find more on Whats wrong with this code? Or get search suggestion and latest updates.