Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    I haven't been using java that long, but I'm currently in a class and we've been given this program to complete; I have been working on this for the last for 3 days now and I keep getting this error:

    Selecting the correct constructor
    Exception in thread "main" java.lang.NullPointerException
    at SlotReservation.main(SlotReservation.java:80)

    referenced code:

    while(answer<1750)
    {
    answer++;
    //Selecting a user randomly from the user table such that he is not in the parking lot and has not been used till now.

    ResultSet rs1=stmt.executeQuery("SELECT MIN(car_regnum), start_time,end_time FROM fsbm_data where temp_present='0' and temp_block='0'");
    if (rs1.next())
    {
    arrivalThreadRegnum = Integer.parseInt(rs1.getString(1).trim());
    startTime = Integer.parseInt(rs1.getString(2).trim());
    endTime = Integer.parseInt(rs1.getString(3).trim());
    stmt.executeUpdate("UPDATE fsbm_data set temp_present=1, temp_block=1 where car_regnum="+rs1.getString(1));

    //Calling thread for each new user and sending his registration number and advance reservation spot number

    new FreeSpaceDetection(userPL.summaryVector,userPL.bit Map,arrivalThreadRegnum, startTime,endTime,userPL.defragList);

    slotNumber++;
    try
    {
    Thread.sleep(1000);
    }
    END OF MY CODE:

    I typed everything as it was presented, but I can't get it to run without this error.
    Any suggestions would be much appreciated...


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Something on that line is null. What is it? Use a debugger or some print statements to figure that out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Thanks for those words

    I have tried debugging, and it found nothing.

    this is the entire page of code:

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Vector;

    public class SlotReservation {

    Vector<Integer> summaryVector = new Vector<Integer>();
    ArrayList<slotInfo> defragList = new ArrayList<slotInfo>();
    int[][] bitMap=new int [48][500];


    public static void main (String args [])
    {

    SlotReservation userPL = new SlotReservation();
    userPL.summaryVector = new Vector<Integer>();
    userPL.defragList = new ArrayList<slotInfo>();
    int answerLimit = 0;
    //Arrival Thread Variable initialization

    int slotNumber = 0;
    int startTime = 0;
    int endTime = 0;
    int arrivalThreadRegnum = 0;

    //Setting up database connection

    Connection conn = null;

    try
    {

    String userName = "root";
    String password = "";
    String url = "jdbc:mysql://localhost/mysql";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
    }
    catch (Exception e)
    {
    System.err.println ("Cannot connect to database server");
    }
    finally
    {
    if (conn != null)
    {
    Statement stmt = null;
    int answer=1;
    try
    {
    stmt = conn.createStatement();
    ResultSet rs=stmt.executeQuery("SELECT MIN(car_regnum), start_time,end_time FROM fsbm_data where temp_present='0' and temp_block='0'");
    if (rs.next())
    {
    System.out.println("Selecting the correct constructor");
    new DefragmentAllocations3(userPL);
    //new DefragmentAllocations2(userPL);
    //new DefragmentAllocations3(userPL);
    }


    while(answer<1750)
    {
    answer++;
    //Selecting a user randomly from the user table such that he is not in the parking lot and has not been used till now.

    ResultSet rs1=stmt.executeQuery("SELECT MIN(car_regnum), start_time,end_time FROM fsbm_data where temp_present='0' and temp_block='0'");
    if (rs1.next())
    {

    arrivalThreadRegnum = Integer.parseInt(rs1.getString(1).trim());
    startTime = Integer.parseInt(rs1.getString(2).trim());
    endTime = Integer.parseInt(rs1.getString(3).trim());
    stmt.executeUpdate("UPDATE fsbm_data set temp_present=1, temp_block=1 where car_regnum="+rs1.getString(1));

    //Calling thread for each new user and sending his registration number and advance reservation spot number

    new FreeSpaceDetection(userPL.summaryVector,userPL.bit Map,arrivalThreadRegnum, startTime,endTime,userPL.defragList);
    slotNumber++;
    try
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    }
    else
    System.out.println("No more users available to simulate the arrival threads");
    }
    }
    catch (SQLException e1)
    {
    e1.printStackTrace();
    }

    }
    }
    System.out.println("DEFRAG LIST AT END OF MAIN IS "+userPL.defragList);

    }

    }


    Complete page of code... Error Still occurring after running debugger...

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    When posting code, please use the highlight tags to preserve formatting. Unformatted code is pretty hard to read.

    A debugger doesn't find or fix anything for you. It simply allows you to examine the values while the code is running. I'd add a break point right before the line that errors, run the debugger, and examine each variable- one of them is null. Alternatively you could use a print statement, like so: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  2. Exception in thread "main" java.lang.NullPointerException
    By StratMaster007 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 12:07 PM
  3. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM
  4. Replies: 1
    Last Post: August 31st, 2011, 10:48 AM
  5. Exception in thread "main" java.lang.NullPointerException
    By MryJaho in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 4th, 2011, 05:36 PM