Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trouble with table model  RSS feed

 
Gulsum Ramazanoglu
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi again, I tried to use a table model, but I have a runtime problem which I couldnt understand why.. The program code is ;

<blockquote>code:
<pre name="code" class="core">
import javax.swing.*;
import javax.swing.table.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.sql.*;
import java.lang.*;


class editing extends JFrame implements ActionListener
{
PreparedStatement stmt;
final String driver = "org.apache.derby.jdbc.EmbeddedDriver";
final String url = "jdbc erby atient_db";
JTable detail_table;
JTextField expense_date;
String[][] daily_details;
editing_table_model etm;
JPanel daily_details_table_panel;

public editing()
{
etm=new editing_table_model();
setDefaultCloseOperation(EXIT_ON_CLOSE);
JLabel expense_date_label=new JLabel("Harcama tarihi : ");
expense_date=new JTextField(8);
expense_date.addActionListener(this);
Action save;
save=new AbstractAction()
{
public void actionPerformed(ActionEvent e)
{
// todo
}
};
save.putValue(Action.NAME,"Kaydet");
JTable detail_table=new JTable(etm);
JScrollPane scroll=new JScrollPane(detail_table);

JPanel date_entry_panel=new JPanel(new BorderLayout());
JPanel daily_details_table_panel=new JPanel(new BorderLayout());
date_entry_panel.add(expense_date,BorderLayout.CENTER);
date_entry_panel.add(expense_date_label,BorderLayout.WEST);
daily_details_table_panel.add(scroll,BorderLayout.NORTH);
daily_details_table_panel.add(new JButton(save),BorderLayout.SOUTH);

getContentPane().add(date_entry_panel,BorderLayout.NORTH);
getContentPane().add(daily_details_table_panel,BorderLayout.CENTER);

pack();
}

public void actionPerformed(ActionEvent event)
{ //getData();
}

public void getData()
{
try
{
Class.forName(driver);
Connection c = DriverManager.getConnection(url);
PreparedStatement stmt=c.prepareStatement("SELECT * FROM daily_expense WHERE EXDATE = ?");
int date = (Integer.valueOf(expense_date.getText())).intValue();
stmt.setInt(1, date);
ResultSet results = stmt.executeQuery();
daily_details=new String[50][6];
int j=0;
while (results.next())
{
for (int i = 0; i < 5; i++)
{
daily_details[j][i] = results.getString(i+1);
}
j++;
}


}
catch (Exception e)
{
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
System.err.println(e.toString());
}
JTable detail_table=new JTable(new etm());
JScrollPane scroll=new JScrollPane(detail_table);
daily_details_table_panel.add(scroll,BorderLayout.NORTH);
getContentPane().add(daily_details_table_panel,BorderLayout.CENTER);
pack();


} // getData method


public static void main(String args[])
{editing frame_instance= new editing();
frame_instance.setVisible(true);
}

} // editing class



class etm extends AbstractTableModel
{
String[][] daily_details;
String[] column_names;

public etm()
{column_names=new String[5];
daily_details=new String[50][6];
String[] column_names={"Sağlayan firma", "Harcamanın niteliği","Miktarı","Fiyatı","Tutarı"};

}

public int getColumnCount()
{return column_names.length;
}

public int getRowCount()
{ int x=5;return x;
}

public String getColumnName(int column)
{return column_names[column];
}

public String getValueAt(int row, int column)
{return daily_details[row][column];
}

public boolean isCellEditable(int row, int column)
{if (column==6) {return false;} else {return true;}
}

public void setValueAt(String new_value,int row, int column)
{daily_details[row][column]=new_value;
fireTableCellUpdated(row,column);
}

}
</pre>
</blockquote>

The runtime problem is telling that the problem comes out at one of the table model methods, getRowCount.. The runtime message is;

Exception in thread "main" java.lang.NullPointerException
at editing_table_model.getRowCount(editing.java:114)
at javax.swing.JTable.getRowCount(Unknown Source)
... messages are a lot, but I placed here just first 3 lines..

NullPointerception is about looking for an instance variable and finding a null value, right? Just to eliminate the runtime problem I placed a constant variable x=5, but still the problem is the same..

I couldnt figure out . would you please shed some light?
Thank you all.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code seems mixed up.

1) For one thing, your TableModel class is called etm, and yet when you try to use it, you are using class editing_table_model with a variable called etm.

2) Your tablemodel overrides AbstractTableModel rather than DefaultTableModel. Are you absolutely sure that you want to do this? This puts a lot of responsibility on you to implement everything.

3) When you extract your data from your ResultSet, you place it in a editing class variable, a 2 dimensional String array called daily_details, but never place this data in the actual tablemodel. The tablemodel on the other hand has a 2 dimensional String array with exactly the same name (daily_details) but that's all that it shares in common with the actual data. They are two completely different objects and never interact with each other.

There are probably more problems here, but this is all I could find on cursory view.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!