开发者

filling arraylist with database values

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package XawalaManager;

import java.sql.*; // DB handling package
import java.util.*;
import javax.swing.table.*;
import javax.swing.table.AbstractTableModel;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.TableModelEvent;

/**
 *
 * @author Abdi Aden
 */
public class DBHandler extends AbstractTableModel {


    private static Connection connection;

    private static Statement stmt;
    Vector columnHeaders;
    Vector tableData;
    static int id;
   public static int autokey = -1;
    static String [] contactList;
    static  ArrayList senders =  new ArrayList();

    public DBHandler() {
        Vector rowData;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            // Assumes Messages.mdb is in the same folder as MessageData.class
            String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=olympic.mdb;";
            connection = DriverManager.getConnection(sourceURL, "admin", "");
            stmt = connection.createStatement();           
            String sql = "Select * FROM senderTable";
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();

            int count = md.getColumnCount();
            columnHeaders = new Vector(count);
           tableData = new Vector();
            for (int i = 1; i <= count; i++) {
                columnHeaders.addElement(md.getColumnName(i));
            }
            while (rs.next()) {
                rowData = new Vector(count);
                for (int i = 1; i <= count; i++) {
                    rowData.addElement(rs.getObject(i));
                }
               tableData.addElement(rowData);
            }


        } catch (Exception e) {
            System.out.println("There is an connection error:  " +e );
        }
    }

    public int getColumnCount() {
        return columnHeaders.size();
    }

    public int getRowCount() {
        return tableData.size();
    }

    public Object getValueAt(int row, int column) {
        Vector rowData = (Vector) (tableData.elementAt(row));
        return rowData.elementAt(column);
    }



    public boolean isCellEditable(int row, int column) {
        return false;
    }

    public String getColumnName(int column) {
        return (String) (columnHeaders.elementAt(column));
    }






    public static ArrayList getSend(){

        try{
            ResultSet res = stmt.executeQuery("SELECT * FROM senderTable");
            //ArrayList senders =  new ArrayList();
            while (res.next()){
                String send = res.getString(2);
                senders.add(send);

            }
        }catch (Exception e ){
            System.out.println("getSend "+e);
            return null;
        }
        return senders;
    }

    // close the database

    public static void close() {
        try {
            connection.close();
        } catch (Exception e) {
            // this shouldn't happen
            System.out.println("close"+e);
        }
    }
}

this my full code my table model words fine fills up my table just fine but the array doesn't full and give me a stack trace :

getSend java.lang.NullPointerException
Exception in thread "main" java.lang.NullPointerException
        at XawalaManager.开发者_运维知识库mainView.<init>(mainView.java:86)
        at XawalaManager.XawalaManager.<init>(XawalaManager.java:40)
        at XawalaManager.XawalaManager.main(XawalaManager.java:108)
Java Result: 1

like this


Comment out this line:

//ArrayList senders =  new ArrayList();

Change type to List and use generics

List<String> senders =  new ArrayList<String>();

Add this lines: (DBUrl is your database address)

Connection conn = null;
Statement  stmt = null;
conn = DriverManager.getConnection("DBUrl");
stmt = con.createStatement();

Here is your modified code:

public static ArrayList getSend(){

  Connection conn = null;
  Statement  stmt = null;
  ResultSet res = null;
  List<String> senders =  new ArrayList<String>();

  try{
    conn = DriverManager.getConnection("DBUrl");
    stmt = con.createStatement();
    res = stmt.executeQuery("SELECT * FROM senderTable");

    while (res.next()){
        String send = res.getString(2);
        senders.add(send);

    }
  }catch (Exception e ){
    System.out.println("getSend "+e);
    return null;
  }
 return senders;
}

If you need more help just post stack trace of your exception.

Edit: Dont have your db drivers and enviroment on my local pc but following code should work.

import java.sql.*;
import java.util.*;
import javax.swing.table.AbstractTableModel;

public class DBHandler extends AbstractTableModel {

    private static Connection connection;
    private static Statement stmt;
    List<String> columnHeaders;
    List<List<String>> tableData;
    static int id;
    public static int autokey = -1;
    static String[] contactList;
    static ArrayList<String> senders = new ArrayList<String>();

    public DBHandler() {
        List rowData = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String sourceURL = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=olympic.mdb;";
            connection = DriverManager.getConnection(sourceURL, "admin", "");
            stmt = connection.createStatement();
            String sql = "Select * FROM senderTable";
            ResultSet rs = stmt.executeQuery(sql);
            ResultSetMetaData md = rs.getMetaData();

            int count = md.getColumnCount();
            columnHeaders = new ArrayList<String>();
            tableData = new ArrayList<List<String>>();
            for (int i = 1; i <= count; i++) {
                columnHeaders.add(md.getColumnName(i));
            }
            while (rs.next()) {
                rowData = new ArrayList<String>();
                for (int i = 1; i <= count; i++) {
                    rowData.add(rs.getObject(i));
                }
                tableData.add(rowData);
            }


        } catch (Exception e) {
            System.out.println("There is an connection error:  " + e);
        }
    }

    public int getColumnCount() {
        return columnHeaders.size();
    }

    public int getRowCount() {
        return tableData.size();
    }

    public Object getValueAt(int row, int column) {
        ArrayList rowData = (ArrayList) (tableData.get(row));
        return rowData.get(column);
    }

    @Override
    public boolean isCellEditable(int row, int column) {
        return false;
    }

    @Override
    public String getColumnName(int column) {
        return (String) (columnHeaders.get(column));
    }

    public static ArrayList getSend() {

        try {
            ResultSet res = stmt.executeQuery("SELECT * FROM senderTable");
            while (res.next()) {
                String send = res.getString(2);
                senders.add(send);
            }
        } catch (Exception e) {
            System.out.println("getSend " + e);
            return null;
        }
        return senders;
    }

    public static void close() {
        try {
            connection.close();
        } catch (Exception e) {
            System.out.println("close" + e);
        }
    }
}


The only objects that can be null is the senders object and stmt object.

A stack trace would determine which one is null.

From your full code listing, you can see in your constructor that you have a local and a global variable with the same name (stmt) this is bad practice, but is not the cause of your error.

Also, you are accessing the statement (stmt) in a static context, but only creating the statement in the constructor (which I cannot determine if it is ever executed). Your mixing of statics and non statics seems very confused.

So, the problem is almost certainly that the stmt object is null, and this is likely to be because you are accessing the getSend method statically, before the constructor is executed. A constructor should NOT be used to set up static variables. Some suggestions would be to

  • look at the Singleton pattern
  • look at static initialisers

Both of these would solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜