Results 1 to 11 of 11
  1. #1
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

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

    I am new to java. And I am keeping receiving this error message. I don't know why this is happening

    Java Code:
    import java.util.*;
    import javax.swing.*;
    /**
     *
     * @author Administrator
     */
    public class CarRegistation {
        private ArrayList CarOwner = null;
        private ArrayList CarRegInfo = null;
    
         public void CarRegistation(){
            CarOwner = new ArrayList();
            CarRegInfo = new ArrayList();
        }
    
         public void newCarRegistation(String oc, String nic, String man, String Reg){
             //try{
                  //PersonsInfo p = new PersonsInfo(oc, nic);
                  PersonsInfo p = new PersonsInfo(oc, nic);
                  Cars c = new Cars(man, Reg);
                  CarOwner.add(p);         //error comes here Null Pointer
                  CarRegInfo.add(c);
             /*}catch (Exception e){
                JOptionPane.showMessageDialog(null, e.getMessage());
          }*/
        }
    
         public void searchCarRegistation (String n) {
            // try{
                 for(int i=0; i<CarOwner.size(); i++){
                     PersonsInfo p = (PersonsInfo)CarOwner.get(i);
                     Cars c = (Cars)CarRegInfo.get(i);
                     if (n.equals(p.getName())){
    //                     WindowsApplication wa = new WindowsApplication();
    //                     wa.txtManufacturer.setText(c.getCarManufacturer());
    //                     wa.txtOwnerOfCar.setText(p.getName());
    //                     wa.txtNicNo.setText(p.getNicNo());
    //                     wa.txtRegistationNo.setText(c.getRegistation());
                    }
                }
            /*}catch (Exception e){
                JOptionPane.showConfirmDialog(null, e.getMessage());
          }*/
        }
    
         public void deleteCarRegistation(String nic){
             /*try{*/
                 for(int i=0; i<CarOwner.size(); i++){
                     PersonsInfo p = (PersonsInfo)CarOwner.get(i);
                     //Cars c = (Cars)CarRegInfo.get(i);
                     if (nic.equals(p.getNicNo())){
                         CarOwner.remove(i);
                         CarRegInfo.remove(i);
                         JOptionPane.showMessageDialog(null, "Car having NIC: " + p.getNicNo() + "has been remove from list");
                     }
                 }
             //}catch (Exception e){
               //  JOptionPane.showConfirmDialog(null, e.getMessage());
             //}
         }
    
         public int countCarRegistation(String n){
             int size = 0;
             //try{
                 for(int i=0; i<CarOwner.size();i++){
                     PersonsInfo p = (PersonsInfo)CarOwner.get(i);
                     if(n.equals(p.getName())){
                         size = size + 1;
                     }
                 }
             //}
             //catch (Exception e){
             //    JOptionPane.showConfirmDialog(null, e.getMessage());
            // }
             return size;
         }
    
    }
    Please help me out as I have to submit this code ASAP. Thanks in advance.

  2. #2
    k4ff1n34dd1c7 is offline Member
    Join Date
    Sep 2008
    Posts
    7
    Rep Power
    0

    Default

    I am guessing it has to do with your arraylists? is pointing into a null position. post you java error stack. Its had to tell without knowing where the error occurs

  3. #3
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Exception in thread "main" java.lang.NullPointerException
    at mytesting.CarRegistation.newCarRegistation(CarRegi station.java:29)
    at mytesting.Main.main(Main.java:29)

    I have four class right now. One of then is PersonalInfo and Cars these are classes which I want to make collection and main class

  4. #4
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    CarOwner is null
    how can a null object call method size

    i cant find constructor in your class

  5. #5
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    public void CarRegistation(){
    CarOwner = new ArrayList();
    CarRegInfo = new ArrayList();
    }

    this is the constructor

  6. #6
    xbox_nutter is offline Member
    Join Date
    Mar 2009
    Posts
    15
    Rep Power
    0

    Default

    just ran a couple of quick tests and this seemed to work

    When declaring your arrays try :
    Java Code:
    private ArrayList<PersonsInfo> CarOwner = new ArrayList<PersonsInfo>();
    And i know it is sometimes easy to over look stuff when passing Variables so make sure the types in the constructor for Car and personInfo accept strings!!
    Last edited by xbox_nutter; 04-02-2009 at 01:16 PM.

  7. #7
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Not Working anyone else help please please do it

  8. #8
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    thx for everyone i have resolve my problem 'mtyoung' is very close i have constructor but its not working for me

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    7

    Default

    public void CarRegistation(){}
    never be a constructor

    above is a void method call CarRegistation

    without self defined constructor, default constructor will initialize your ArrayList
    Last edited by mtyoung; 04-03-2009 at 06:58 AM.

  10. #10
    Nisha18 is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default Constructor doesnt have a return type

    myToung is right

  11. #11
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    thx myToung, Nisha and everyone else who help to solve my problem.

Similar Threads

  1. Replies: 15
    Last Post: 10-12-2010, 06:51 PM
  2. Exception in thread "main" java.lang.NullPointerException
    By jyothi.priyanka in forum New To Java
    Replies: 11
    Last Post: 03-11-2009, 04:18 AM
  3. Exception in thread "main" java.lang.NullPointerException
    By vasavi.singh in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 02:19 PM
  4. Replies: 1
    Last Post: 02-17-2009, 11:54 AM
  5. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

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