Results 1 to 5 of 5
  1. #1
    javastarter1 is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default java.lang.NullPointerException

    Hi, I am just learning Java, when I run my programme in eclipse I get a message at the side saying 'java.lang.NullPointerException' does anybody know how to help?
    thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: java.lang.NullPointerException

    Probably - (but we would need more info. Like, the code). And please paste the code between code tags to properly format it.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    javastarter1 is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: java.lang.NullPointerException

    Thanks for replying here is the code I am trying to execute. I am trying to design a database to calculate employee holidays with regards to age and years of service.
    When I run the code the GUI appears on screen with an error message saying 'java.lang.NullPointerException'
    As you can see there is quite a bit of code below and I am trying to work out which line is causing the error.
    Thanks for any help!

    Java Code:
    import java.sql.*;
    
    //class to connect to database
    
    public class DB {
    
    //defined class objects 
    
    Connection connection;
    Statement statement;
    ResultSet resultset;
    
    //created method to call connect 
    
    public DB(){
    
    connectToDatabase();
    
    }
    
    //created method to define database connection 
    
    public void connectToDatabase(){
    
    //Connect to database
    
    try{
    
    String databaseDriver = "sun.jDBc.oDBc.JDBcODBcDriver";
    
    Class.forName(databaseDriver);
    
    String databaseReferenceName = "jDBc:oDBc:MS Access Database";
    
    connection = DriverManager.getConnection(databaseReferenceName);
    
    statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
    
    String sqlCommand = "select * from Tbl_employee_holidays";
    
    resultset = statement.executeQuery(sqlCommand);
    
    //If error occurs catch and display in messagebox 
    
    }
    
    catch(Exception ex){
    
    }
    
    }   
    //Main method to call database and GUI class 
    
    public static void main(String[] args) {
    
    new DB();
    
    new GUI();
    
    }
    
    }
    import javax.swing.*;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.event.*;
    import javax.swing.JPanel;
    import javax.swing.JButton;
    
    //class GUI extends the Database class by creating a Graphical User Interface 
    
    public class GUI extends DB {
    
    //Variable declaration
    
    int age;
    int service;
    int additionalDaysHoliday = 0;
    int multiplyfactor = 0;
    
    //create frame, (window), to hold labels, textboxes and buttons for the GUI 
    
    JFrame jframe_window;
    
    //defined java labels (jl) - title, employee ID, employee name, employee age, employee service and employee additional holidays 
    
    JLabel jl_Title;
    
    JLabel jl_Employee_ID;
    JLabel jl_Employee_Name;
    JLabel jl_Employee_Age;
    JLabel jl_Employee_Service;
    JLabel jl_Employee_Additional_Holidays; 
    
    //define java text fields (Textboxes - jtf) - employee ID, employee name, employee age, employee service and employee additional holidays 
    
    JTextField jtf_Employee_ID;
    JTextField jtf_Employee_Name;
    JTextField jtf_Employee_Age;
    JTextField jtf_Employee_Service;
    JTextField jtf_Employee_Additional_Holidays;
    
    //define User Control Buttons 
    
    //record navigation buttons
    
    JButton jb_Next = new JButton("Next");
    JButton jb_Prev = new JButton("Previous");
    JButton jb_Last = new JButton("Last");
    JButton jb_First = new JButton("First");
    
    //record management buttons
    
    JButton jb_update = new JButton("Update");
    JButton jb_delete = new JButton("Delete");
    JButton jb_new = new JButton("New");
    JButton jb_save = new JButton ("Save");
    
    //created method which calls frame - to create and display frame (window) and listen for User actions using the tbtnAction method 
    
    public GUI() {
    
    //call to create and draw frame (window including elements to screen) 
    frame();
    
    //call to listen for User actions on the screen buttons
    listenForUserAction();
    
    }
    //frame method creates and holds the properties of frame, labels, textboxes, buttons and panel 
    
    public void frame(){
    
    //set frame properties 
    
    jframe_window = new JFrame();
    
    jframe_window.setTitle("Java Employee Additional Holiday Calculator - Created By Conor O'Neill");
    jframe_window.setSize(600, 400);
    jframe_window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    //define fonts - font type, font style and point size 
    
    Font font0 = new Font("Arial", Font.BOLD ,12);
    
    Font font1 = new Font ("Arial", Font.BOLD , 18);
    
    //Create JLabels to display Field Names
    //Set location, size and font of labels
    
    jl_Title = new JLabel("Employee Additional Holiday Calculator");
    jl_Title.setLocation(100,10);
    jl_Title.setSize (400,20);
    jl_Title.setFont(font1);
    jl_Title.setForeground(Color.BLUE);
    
    jl_Employee_ID = new JLabel("Employee ID");
    jl_Employee_ID.setLocation(10, 40);
    jl_Employee_ID.setSize(200, 20);
    jl_Employee_ID.setFont(font0);
    
    jl_Employee_Name = new JLabel("Employee Name");
    jl_Employee_Name.setLocation(10, 80);
    jl_Employee_Name.setSize(200, 20);
    jl_Employee_Name.setFont(font0);
    
    jl_Employee_Age = new JLabel("Employee Age");
    jl_Employee_Age.setLocation(10, 120);
    jl_Employee_Age.setSize(200, 20);
    jl_Employee_Age.setFont(font0);
    
    jl_Employee_Service = new JLabel("Employee Service");
    jl_Employee_Service.setLocation(10, 160);
    jl_Employee_Service.setSize(200, 20);
    jl_Employee_Service.setFont(font0);
    
    jl_Employee_Additional_Holidays = new JLabel("Additional Holidays Due"); 
    jl_Employee_Additional_Holidays.setLocation(10, 200);
    jl_Employee_Additional_Holidays.setSize(200, 20);
    jl_Employee_Additional_Holidays.setFont(font0);
    
    //Create New JTextField containers to hold Field values
    //Set location, size, font and tooltip text for textboxes
    
    jtf_Employee_ID = new JTextField(10);
    jtf_Employee_ID.setLocation(200, 40);
    jtf_Employee_ID.setSize(200, 20);
    jtf_Employee_ID.setFont(font0);
    jtf_Employee_ID.setToolTipText("READ ONLY FIELD - Displays Employee ID");
    jtf_Employee_ID.setEditable(false);
    
    jtf_Employee_Name = new JTextField(10);
    jtf_Employee_Name.setLocation(200, 80);
    jtf_Employee_Name.setSize(200, 20);
    jtf_Employee_Name.setFont(font0);
    jtf_Employee_Name.setToolTipText("Use field to Enter or Edit Employee Name");
    
    jtf_Employee_Age = new JTextField(10);
    jtf_Employee_Age.setLocation(200, 120);
    jtf_Employee_Age.setSize(200, 20);
    jtf_Employee_Age.setFont(font0);
    jtf_Employee_Age.setToolTipText("Use field to Enter or Edit Employee Age");
    
    jtf_Employee_Service = new JTextField(10);
    jtf_Employee_Service.setLocation(200, 160);
    jtf_Employee_Service.setSize(200, 20);
    jtf_Employee_Service.setFont(font0);
    jtf_Employee_Service.setToolTipText("Use field to Enter or Edit Employee Years of Service");
    
    jtf_Employee_Additional_Holidays = new JTextField(10);
    jtf_Employee_Additional_Holidays.setLocation(200, 200);
    jtf_Employee_Additional_Holidays.setSize(200, 20);
    jtf_Employee_Additional_Holidays.setFont(font0);
    jtf_Employee_Additional_Holidays.setToolTipText("READ ONLY FIELD - Displays Additional Holidays Calculated");
    jtf_Employee_Additional_Holidays.setEditable(false);
    
    //Create JPanel to hold JFrame container with Screen Controls
    //set layout and location of panel
    
    JPanel jpanel = new JPanel(new BorderLayout());
    jpanel.setLayout(null);
    jpanel.setBackground(Color.PINK);
    
    //Add Field TextBoxes and Labels to JPanel
    
    jpanel.add(jl_Title);
    
    jpanel.add(jl_Employee_ID);
    jpanel.add(jtf_Employee_ID);
    
    jpanel.add(jl_Employee_Name);
    jpanel.add(jtf_Employee_Name);
    
    jpanel.add(jl_Employee_Age);
    jpanel.add(jtf_Employee_Age);
    
    jpanel.add(jl_Employee_Service);
    jpanel.add(jtf_Employee_Service);
    
    jpanel.add(jl_Employee_Additional_Holidays);
    jpanel.add(jtf_Employee_Additional_Holidays);
    
    //set tooltip background and text colour 
    
    try
    
    {
    
    UIManager.put("Tooltip.background", Color.WHITE);
    UIManager.put("ToolTip.foreground", Color.RED);
    
    }
    
    catch(Throwable th)
    
    {
    
    th.printStackTrace();
    
    }
    
    //Add Navigation Buttons to JPanel and set their position, size and tooltip
    
    jb_Next.setBounds(420, 40, 130, 30);
    jb_Next.setToolTipText("Click this button to view Next Employee Record");
    
    jb_Prev.setBounds(420, 80, 130, 30);
    jb_Prev.setToolTipText("Click this button to view Previous Employee Record");
    
    jb_Last.setBounds(420, 120, 130, 30);
    jb_Last.setToolTipText("Click this button to view Last Employee Record");
    
    jb_First.setBounds(420, 160, 130, 30);
    jb_First.setToolTipText("Click this button to view First Employee Record");
    
    jb_update.setBounds(10, 240, 130, 30); 
    jb_update.setToolTipText("Click this button to Save Updated Employee Information");
    
    jb_delete.setBounds(150, 240, 130, 30); 
    jb_delete.setBackground(Color.RED);
    jb_delete.setToolTipText("Click this button to Delete Employee Record");
    
    jb_new.setBounds(290, 240, 130, 30);
    jb_new.setToolTipText("Click this button to clear fields to add New Employee Information");
    
    jb_save.setBounds(430, 240, 130, 30);
    jb_save.setBackground(Color.GREEN);
    jb_save.setToolTipText("Click this button to Save New Employee Information");
    
    //Add Buttons to JPanel
    
    jpanel.add(jb_Next);
    jpanel.add(jb_Prev);
    jpanel.add(jb_Last);
    jpanel.add(jb_First);
    jpanel.add(jb_update);
    jpanel.add(jb_delete);
    jpanel.add(jb_new);
    jpanel.add(jb_save);
    
    //Add JPanel to JFrame
    
    jframe_window.add(jpanel);
    
    //Once jpanel has properties set and added to Jframe_window make the frame visible
    
    jframe_window.setVisible(true);
    
    //Connect to the MS Access DB, populate the ResultSet with all data, 
    //navigate to first record, populate screen controls
    
    try{
    
    resultset.next();
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, ex); 
    
    } 
    
    }
    
    public static int calculateAdditionalHolidays(int Age, int Service){
    
    int age = Age;
    int service = Service;
    
    //age = ConvertStringToInt(jtf_Employee_Age.getText());
    //service = ConvertStringToInt(jtf_Employee_Service.getText());
    
    int extraDaysAge = 0;
    int extraDaysYears = 0;
    
    //Determine how many extra days based on Age
    
    if (age > 15 & age < 21)
    
    {
    
    extraDaysAge = 0;
    
    }
    
    if (age > 20 & age < 31)
    
    {
    
    extraDaysAge = 1;
    
    }
    
    if (age > 30 & age < 41)
    
    {
    
    extraDaysAge = 2;
    
    }
    
    if (age > 40 & age < 51)
    
    {
    
    extraDaysAge = 3;
    
    }
    
    if (age > 50 & age < 61)
    
    {
    
    extraDaysAge = 4;
    
    }
    
    if (age > 60)
    
    {
    
    extraDaysAge = 5;
    
    }
    
    //Determine how many extra days based on Years service
    
    if (service < 6)
    
    {
    
    extraDaysYears = 0;
    
    }
    
    if (service > 5 & service < 11)
    
    {
    
    extraDaysYears = 1;
    
    }
    
    if (service > 10 & service < 16)
    
    {
    
    extraDaysYears = 2;
    
    }
    
    if (service > 15 )
    
    {
    
    extraDaysYears = 3;
    
    }
    
    int hols = (extraDaysAge * extraDaysYears);
    
    return hols;
    
    }
    
    public static int ConvertStringToInt(String aString){
    
    int aInt = Integer.parseInt(aString);
    
    return aInt;
    
    }
    
    public static double ConvertStringToDouble(String aString){
    
    double aDouble = Double.parseDouble(aString);
    
    return aDouble;
    
    }
    
    public static String ConvertIntToString(int aInt){
    
    String aString = Integer.toString(aInt);
    
    return aString;
    
    }
    
    public static String ConvertDoubleToString(Double aDouble){
    
    String aString = Double.toString(aDouble);
    
    return aString;
    
    }
    
    //Create Button Method which 'listens' for Users Clicking each button and responds accordingly 
    
    public void listenForUserAction(){
    
    jb_Next.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Connect with database and search for next record 
    
    try{
    
    if(resultset.next())
    
    {
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    }
    
    else
    
    {
    
    resultset.previous();
    
    //if no next record display messagebox
    
    JOptionPane.showMessageDialog(null,"No More Employee Information!");
    
    }
    
    //if error catch and display in messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, ex); 
    
    }
    
    }
    
    });
    
    jb_Prev.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Connect to database and search for previous record 
    
    try{
    
    if(resultset.previous())
    
    {
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    }
    
    else
    
    {
    
    //if no previous records display messagebox 
    
    resultset.next();
    
    JOptionPane.showMessageDialog(null,"No More Employee Information!");
    
    }
    
    //if error occurs catch and display in messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    }
    
    }
    
    });
    
    jb_Last.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Connect to database and search for last record 
    
    try{
    
    resultset.last();
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    //if error occurs catch and display in messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    }
    
    }
    
    });
    
    jb_First.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Connect to database and search for first record
    
    try{
    
    resultset.first();
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    //If error occurs catch and display in messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, ex);
    
    }
    
    }
    
    });
    
    jb_update.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //String product_id = t1.getText(); - The Primary Key value is never modified by a User
    
    String employee_name = jtf_Employee_Name.getText();
    
    String employee_age = jtf_Employee_Age.getText().replaceAll(" ", "");
    String employee_service = jtf_Employee_Service.getText().replaceAll(" ", "");
    
    jtf_Employee_Additional_Holidays.setText(ConvertIntToString(calculateAdditionalHolidays(ConvertStringToInt(employee_age), ConvertStringToInt(employee_service)))); 
    
    String employee_addhols = jtf_Employee_Additional_Holidays.getText();
    
    //String employee_addhols = jtf_Employee_Additional_Holidays.getText();
    
    //Connect to database and insert information from textboxes
    
    try{
    
    //resultset.updateString("product_ID", product_ID); - The Primary Key value is never modified by a User 
    
    resultset.updateString("employee_name", employee_name);
    resultset.updateString("employee_age", employee_age);
    resultset.updateString("employee_service", employee_service);
    resultset.updateString("employee_addhols", employee_addhols);
    resultset.updateRow();
    
    JOptionPane.showMessageDialog(null, "Record Updated!");
    
    //If error occurs catch and display messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, ex);
    
    }
    
    }
    
    }); 
    
    jb_delete.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Connect to database delete record 
    
    try{
    
    resultset.deleteRow();
    
    resultset.beforeFirst();
    
    resultset.next();
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    //If error occurs catch and display messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, "Delete Error: " + ex);
    
    }
    
    }
    
    });
    
    jb_new.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //Clear text field text
    
    jtf_Employee_ID.setText(" ");
    jtf_Employee_Name.setText(" ");
    jtf_Employee_Age.setText(" ");
    jtf_Employee_Service.setText(" ");
    jtf_Employee_Additional_Holidays.setText(" ");
    
    }
    
    });
    
    jb_save.addActionListener(new ActionListener(){
    
    public void actionPerformed(ActionEvent e)
    
    {
    
    //String product_ID = t1.getText(); - The Primary Key value is never created by a User 
    
    String employee_name = jtf_Employee_Name.getText();
    String employee_age = jtf_Employee_Age.getText().replaceAll(" ", "");
    String employee_service = jtf_Employee_Service.getText().replaceAll(" ", "");
    
    jtf_Employee_Additional_Holidays.setText(ConvertIntToString(calculateAdditionalHolidays(ConvertStringToInt(employee_age), ConvertStringToInt(employee_service)))); 
    
    String employee_addhols = jtf_Employee_Additional_Holidays.getText();
    //Connect to database and enter data from textboxes
    
    try{
    
    resultset.moveToInsertRow();
    
    resultset.updateString("employee_name", employee_name);
    resultset.updateString("employee_age", employee_age);
    resultset.updateString("employee_service", employee_service);
    resultset.updateString("employee_addhols", employee_addhols);
    
    resultset.insertRow();
    
    //Navigate to before first record
    
    resultset.beforeFirst();
    
    //Navigate to first record and populate screen controls 
    
    resultset.next();
    
    jtf_Employee_ID.setText(resultset.getString("employee_ID"));
    jtf_Employee_Name.setText(resultset.getString("employee_name"));
    jtf_Employee_Age.setText(resultset.getString("employee_age"));
    jtf_Employee_Service.setText(resultset.getString("employee_service"));
    jtf_Employee_Additional_Holidays.setText(resultset.getString("employee_addhols"));
    
    JOptionPane.showMessageDialog(null, "Record Saved");
    
    //If error occurs catch and display messagebox
    
    }
    
    catch(Exception ex)
    
    {
    
    JOptionPane.showMessageDialog(null, ex);
    
    } 
    
    }
    
    });
    
    }
    }

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: java.lang.NullPointerException

    Please edit your code and paste the error message at the top. That is also required to assist. And I failed to tell you to try and create the problem in a smaller example. See the SSCCE link in my signature.

    Regards,
    Jim
    Last edited by jim829; 08-04-2013 at 12:37 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    728
    Rep Power
    2

    Default Re: java.lang.NullPointerException

    Quote Originally Posted by javastarter1 View Post
    Thanks for replying here is the code I am trying to execute. I am trying to design a database to calculate employee holidays with regards to age and years of service.
    When I run the code the GUI appears on screen with an error message saying 'java.lang.NullPointerException'
    As you can see there is quite a bit of code below and I am trying to work out which line is causing the error.
    Thanks for any help!
    Post the Exception! We can't sift through 800 lines of code just to guess what is wrong.

Similar Threads

  1. PLEASE HELP ME-i got java.lang.NullPointerException
    By nandhinianand in forum NetBeans
    Replies: 4
    Last Post: 08-05-2011, 06:35 AM
  2. java.lang.NullPointerException
    By stevenhaynes5 in forum New To Java
    Replies: 7
    Last Post: 05-16-2011, 03:01 PM
  3. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM
  4. java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 1
    Last Post: 02-27-2009, 12:36 PM
  5. java.lang.NullPointerException
    By ravian in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •