1

I am new in Java and I was trying to do this exercise; but I just stuck at this new issue. I googled about this error all day long- Java.lang.NullPointerException -and I tried to solve it but I can't find it; anyone please?

** I have a BankDB_Final.db file; and it have a table called: Bank_001 ** I have installed sqlite connector ( the connection is ok) and rs2xml.jar file in the same folder called "lib". ** Login: Abbas .. Password: root.

Here is the methods code: // Launch the application.

import java.awt.*;
import java.sql.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class Login_jFrame extends JFrame {

Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;

private JPanel contentPane;
private JPasswordField txt_password;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            try {
                Login_jFrame frame = new Login_jFrame();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public Login_jFrame() {

    conn = javaconnect.ConnectrDB();


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblNewLabel = new JLabel("Username");
    lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel.setBounds(177, 93, 70, 14);
    contentPane.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("Password");
    lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 12));
    lblNewLabel_1.setBounds(177, 139, 70, 14);
    contentPane.add(lblNewLabel_1);

    txt_password = new JPasswordField();
    txt_password.setBounds(253, 133, 100, 20);
    contentPane.add(txt_password);

    TextField txt_username = new TextField();
    txt_username.setBounds(253, 85, 100, 22);
    contentPane.add(txt_username);

    JPanel panel = new JPanel();
    panel.setBorder(new TitledBorder(new TitledBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Login", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 0, 0)), "Login:", TitledBorder.LEADING, TitledBorder.TOP, null, new Color(64, 64, 64)), "Login:", TitledBorder.LEADING, TitledBorder.TOP, null, Color.DARK_GRAY));
    panel.setBounds(160, 50, 219, 160);

    contentPane.add(panel);
    panel.setLayout(null);

    JButton cmdLogin = new JButton("Login");
    cmdLogin.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            String sql = "select * from bank_001 where AccountName=? and Password=?";
            try{
                pst = conn.prepareStatement(sql);
                pst.setString(1, txt_username.getText());
                pst.setString(2, txt_password.getText());

                rs= pst.executeQuery();
                if(rs.next()) {
                    Employee_info s = new Employee_info();
                    s.setVisible(true);

                    JOptionPane.showMessageDialog(null, "Username and Password is correct!");

                }else {
                    JOptionPane.showMessageDialog(null, "Username or Password is Not correct!");
                }

            }catch(Exception e) {

                JOptionPane.showMessageDialog(null, e);

            }

        }
    });
    cmdLogin.setBounds(90, 126, 89, 23);
    panel.add(cmdLogin);

    JLabel icon = new JLabel("");
    icon.setIcon(new ImageIcon("C:\\Users\\User\\Downloads\\DB SQLITE\\Project123\\img\\icon.jpg"));
    icon.setBounds(40, 56, 90, 90);
    contentPane.add(icon);
}

}

JavaConnect Class:

import java.sql.*;

import javax.swing.*;


public class javaconnect {

Connection conn = null;

public static Connection ConnectrDB() {

    // 1- Get a connection to Database
    try {

        Class.forName("org.sqlite.JDBC");
        Connection conn = DriverManager.getConnection("jdbc:sqlite:c:\\users\\user\\downloads\\db sqlite\\bankdb_final.db");
        //Connection conn = DriverManager.getConnection("jdbc:sqlite:C:\\Users\\User\\Downloads\\DB SQLITE\\BankDB_Final.db");

        JOptionPane.showMessageDialog(null, "Connection Established");

        return conn;
    } catch(Exception e) {

     JOptionPane.showMessageDialog(null, e);
   return null;

    }

}

}

and Finally (I think here is the Error) Employee_info Class

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.border.LineBorder;

import java.awt.Color;

import javax.swing.table.DefaultTableModel;

import java.sql.*;
import javax.swing.*;

import net.proteanit.sql.DbUtils;                 

public class Employee_info extends JFrame {



private JPanel contentPane;
private JTable table_employee;



/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                Employee_info frame = new Employee_info();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Connection conn = null;
ResultSet rs = null;
PreparedStatement pst = null;             

/**
 * Create the frame.
 */
public Employee_info() {


    conn = javaconnect.ConnectrDB();
    Update_Table();


    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setBounds(21, 11, 389, 230);
    contentPane.add(scrollPane);

    table_employee = new JTable();
    scrollPane.setViewportView(table_employee);
    table_employee.setModel(new DefaultTableModel(
        new Object[][] {
        },
        new String[] {
        }
    ));
}


private void Update_Table() {
    try {

    String sql = "select * from Bank_001";
    pst = conn.prepareStatement(sql);
    rs = pst.executeQuery();
    table_employee.setModel(DbUtils.resultSetToTableModel(rs));
    conn.close();

    }catch(Exception e) {
         JOptionPane.showMessageDialog(null, e);

    }

}

}

Screen :

enter image description here

Paulo Zumach
  • 239
  • 1
  • 2
  • 10
  • debug your programe and find what is null and add `e.printStrackTrace();` to get full error details . – Madhawa Priyashantha Aug 04 '15 at 09:48
  • 1) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Aug 04 '15 at 09:50
  • .. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Aug 04 '15 at 09:50
  • 1
    5) use `getPassword()` instead of `getText()` for password field – Madhawa Priyashantha Aug 04 '15 at 09:52
  • Thanks for the tip #Fast Snail . I did it and it pointed to this line: table_employee.setModel(DbUtils.resultSetToTableModel(rs)); Now I will try to discover what that means. – Paulo Zumach Aug 04 '15 at 10:13
  • I added just this line to **Employee_info.Class** --- instead of `private JTable table_employee;` I put `private JTable table_employee = new JTable();` and then I changed in **Windows-Preferences - Java Installed JREs to JDk**. Now I don't have error msgs but it still doesn't displays data. please, any clue? I don't have any idea what to do next. – Paulo Zumach Aug 07 '15 at 09:49
  • @FastSnail please take a look – Paulo Zumach Aug 07 '15 at 10:11
  • @AndrewThompson please take a look, if you have a bit time. – Paulo Zumach Aug 07 '15 at 10:11
  • 1) An MCVE needs to be **one source file** (possibly with more than one class, but only one **`public`** class). 2) We cannot compile it without `net.proteanit.sql.DbUtils` & cannot run it without a DB, try to reproduce the problem with hard coded data. 3) `TextField txt_username = new TextField();` Don't mix AWT components with Swing components. It is a recipe for disaster. 4) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or .. – Andrew Thompson Aug 07 '15 at 10:26
  • .. or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 5) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Aug 07 '15 at 10:26
  • 1
    6) If you had read the content of point 1 of my first comment you should have realised the value of printing the stack trace as suggested by @FastSnail. Since you either did not read those, or are so lacking in comprehension skills that you could not do better at solving the problem yourself, *or* reporting enough so that anyone else can, I am voting to close this question. – Andrew Thompson Aug 07 '15 at 10:32

0 Answers0