Results 1 to 11 of 11
- 04-01-2009, 02:59 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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
Please help me out as I have to submit this code ASAP. Thanks in advance.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; } }
- 04-01-2009, 07:25 PM #2
Member
- Join Date
- Sep 2008
- Posts
- 7
- Rep Power
- 0
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
- 04-02-2009, 09:04 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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
- 04-02-2009, 09:26 AM #4
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
CarOwner is null
how can a null object call method size
i cant find constructor in your class
- 04-02-2009, 11:08 AM #5
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
public void CarRegistation(){
CarOwner = new ArrayList();
CarRegInfo = new ArrayList();
}
this is the constructor
- 04-02-2009, 12:11 PM #6
Member
- Join Date
- Mar 2009
- Posts
- 15
- Rep Power
- 0
just ran a couple of quick tests and this seemed to work
When declaring your arrays try :
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!!Java Code:private ArrayList<PersonsInfo> CarOwner = new ArrayList<PersonsInfo>();
Last edited by xbox_nutter; 04-02-2009 at 12:16 PM.
- 04-02-2009, 01:21 PM #7
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Not Working anyone else help please please do it
- 04-02-2009, 01:33 PM #8
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
thx for everyone i have resolve my problem 'mtyoung' is very close i have constructor but its not working for me
- 04-03-2009, 05:16 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
public void CarRegistation(){}
never be a constructor
above is a void method call CarRegistation
without self defined constructor, default constructor will initialize your ArrayListLast edited by mtyoung; 04-03-2009 at 05:58 AM.
- 04-03-2009, 05:43 AM #10
Member
- Join Date
- Apr 2009
- Posts
- 2
- Rep Power
- 0
Constructor doesnt have a return type
myToung is right
- 04-03-2009, 06:31 AM #11
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Exception in thread "main" java.lang,NullPointerException
By ljk8950 in forum AWT / SwingReplies: 15Last Post: 10-12-2010, 05:51 PM -
Exception in thread "main" java.lang.NullPointerException
By jyothi.priyanka in forum New To JavaReplies: 11Last Post: 03-11-2009, 03:18 AM -
Exception in thread "main" java.lang.NullPointerException
By vasavi.singh in forum New To JavaReplies: 0Last Post: 02-24-2009, 01:19 PM -
Exception in thread "main" java.lang.NullPointerException
By Manfizy in forum New To JavaReplies: 1Last Post: 02-17-2009, 10:54 AM -
ArrayList: Exception in thread "main" java.lang.NullPointerException
By susan in forum New To JavaReplies: 1Last Post: 07-16-2007, 06:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks