Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7920081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:05:01+00:00 2026-06-03T16:05:01+00:00

i have a problem with this exception. I’m trying to make a function to

  • 0

i have a problem with this exception.
I’m trying to make a function to display data from database on a table, every keystroke i made at jtextfield. So its like the table automatically refreshed with new data everytime i type in the jtextfield.

Here’s the code :

First i have this variable :

private Statement stmt;
List<Barang> dataSBarang =new LinkedList();
boolean searchBarang=true;
Searching sBarang;

And this is how i call the function :

private void inputkodeTFMouseClicked(java.awt.event.MouseEvent evt){                                         
    sBarang = new Searching( stmt, dataSBarang, modelDetail, tabelDetailBarang, inputkodeTF, searchBarang);
    sBarang.start();
}  

And this is the Searching Object

public class Searching extends Thread{
private Statement stmt;
private List<Barang> dataBarang;
private JTable jTabelBarang;
private JTextField tf;
private boolean cari;
private DefaultTableModel modelBarang;

public Searching(Statement stmt, List<Barang> dataBarang, DefaultTableModel tm, JTable jTabelBarang, JTextField tf, boolean cari){
   this.stmt=stmt;
   this.dataBarang=dataBarang;
   this.modelBarang=tm;
   this.jTabelBarang=jTabelBarang;
   this.tf=tf;
   this.cari=cari;

}


@Override
public void run(){
    String temp="";
    while(cari==true){
    //System.out.println("jalan");
    try{
        String masukan = tf.getText();
        System.out.println(masukan);
    if(!masukan.equals("")&&!masukan.equals(temp)){
        clearTableBarang();
        //System.out.println("Mencari "+ masukan);
        ResultSet rs = stmt.executeQuery("select kode_barang, nama_barang, jumlah_stok, " +
            "minimal_stok, harga_jual, deskripsi_barang from BARANG WHERE (kode_barang LIKE " +
            "'"+masukan+"%')");
        System.out.println(rs);

        while(rs.next()){
            String kode_barang = rs.getString ("kode_barang");
            String nama_barang = rs.getString ("nama_barang");
            int jumlah_stok = rs.getInt("jumlah_stok");
            int minimal_stok = rs.getInt("minimal_stok");
            int harga_jual = rs.getInt("harga_jual");
            String deskripsi_barang = rs.getString ("deskripsi_barang");
            //System.out.println(kode_barang+" "+deskripsi_barang);

            dataBarang.add(new Barang(kode_barang,nama_barang,jumlah_stok,minimal_stok,harga_jual,deskripsi_barang));

            ((DefaultTableModel) jTabelBarang.getModel()).insertRow(jTabelBarang.getRowCount(), new Object[]{kode_barang, nama_barang, jumlah_stok, minimal_stok, harga_jual, deskripsi_barang});



        }
        temp = masukan;
    }
    else if(masukan.equals("")&&!masukan.equals(temp)) {
        clearTableBarang();
        showTableBarang();
        temp = masukan;
    }
    } catch(SQLException s){s.printStackTrace();}
    catch(ArrayIndexOutOfBoundsException s){s.printStackTrace();}
    try {
        sleep(500);

} catch(InterruptedException e){}

    }
}
public void clearTableBarang(){
    int numrows = modelBarang.getRowCount();
    for(int i = numrows - 1; i >=0; i--){
        modelBarang.removeRow(i);
    }

    dataBarang.clear();


}
public void showTableBarang(){
    try{
    ResultSet rs = stmt.executeQuery("select kode_barang, nama_barang, jumlah_stok, minimal_stok, harga_jual, deskripsi_barang from barang");
    while(rs.next()){
            String kode_barang = rs.getString ("kode_barang");
            String nama_barang = rs.getString ("nama_barang");
            int jumlah_stok = rs.getInt("jumlah_stok");
            int minimal_stok = rs.getInt("minimal_stok");
            int harga_jual = rs.getInt("harga_jual");
            String deskripsi_barang = rs.getString ("deskripsi_barang");
            //System.out.println(kode_barang+" "+deskripsi_barang);

            dataBarang.add(new Barang(kode_barang,nama_barang,jumlah_stok,minimal_stok,harga_jual,deskripsi_barang));

            ((DefaultTableModel)jTabelBarang.getModel()).insertRow(jTabelBarang.getRowCount(), new Object[]{kode_barang, nama_barang, jumlah_stok, minimal_stok, harga_jual, deskripsi_barang});

    }
    } catch(SQLException s){s.printStackTrace();}
}
public void delay(){
    try {
        sleep(1000000000);

} catch(InterruptedException e){}
}

}

This is the error :

Exception in thread "Thread-4" java.lang.NullPointerException
    at mypkg.Searching.run(Searching.java:47)

FYI : Line 47 is pointing to

ResultSet rs = stmt.executeQuery("select kode_barang, nama_barang, jumlah_stok, " +
"minimal_stok, harga_jual, deskripsi_barang from BARANG WHERE (kode_barang LIKE " +
"'"+masukan+"%')");

Please help me solve the problem. Thank you very much. 😀

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T16:05:04+00:00Added an answer on June 3, 2026 at 4:05 pm

    NullPointerExceptions are the most easy ones to fix with a debugger. Just place a breakpoint on that line and see what is null.

    If the line you posted is correct, you do not even need a debugger since the only thing that can throw the exception is stmt which will be null.

    Note:

    • It is a good thing to run your DB query in a separate Thread to avoid blocking the UI. However, in your case you are updating the UI from that Thread which is not allowed and will cause weird issues. All Swing components must be accessed on the Event Dispatch Thread (EDT). Consult the Swing concurrency tutorial for more information
    • I do hope you are not starting up a separate Thread on each keystroke in the textfield as you indicated in your question. Looking at the code in the Thread, you remove all elements from the table and then re-add rows. So if a users types in 5 characters at a normal typing speed, you will launch 5 threads which most likely run all at the same time (since a DB connection might not be that fast if your network is lagging). That means that with your current code 5 Threads are, at the same time, removing the table model and adding rows. Even if you put all the Swing code on the EDT (see my first point), you still end up with 5 threads posting runnables on the EDT messing with your table model. I do not know what the resulting table model will be, but probably not what you want
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with this exception: Hibernate.HibernateException : Could not create the driver from
I have problem with this code: file = tempfile.TemporaryFile(mode='wrb') file.write(base64.b64decode(data)) file.flush() os.fsync(file) # file.seek(0)
I have this problem I've been trying to tackle for a while. I have
I have a problem with this code. It's generates this exception: Text' threw an
Hi i have problem with this code, i found it on the internet and
i am a beginner and i have a problem : this code doesnt compile
I have this problem where I open Visual Studio and the internal windows are
i have this problem to find a particular xml node l have post this
i have this problem: ms-access could not delete and i found a potential solution
I have this problem in my ASP.NET application where I'm seeing some of my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.