Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 22nd, 2010, 03:10 PM
yotamoo yotamoo is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2010
Posts: 8 yotamoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 26 sec
Reputation Power: 0
Homework - Exception in thread "main" java.lang.NullPointerException

So, i am trying to build a simple database (array of Car type).
the code is quite simple but for some reason i keep getting the same error after running the program (compiling went through). so i have 3 classes:

PHP Code:
class DataBase {
    
    private 
Car[] database = new Car[3];

    public 
DataBase() {
    for (
int i=0i<database.lengthi++) {
        
database[i].formatCar();
    }
    
    } 
    
    public 
void addCar(Car car1) {
    for (
int i=0i<database.lengthi++) {
        if (
database[i].getModel()=="empty") {
        
database[i]=car1;
        }
    }
    } 

    public 
void printDataBase() {
    
int i=0;
    while (
i<database.length) {
        
database[0].printCar();
        
i=i+1;
    }
    
    }




PHP Code:
class mainpage {

    public static 
void main (String[] args) {
    
Car car1 = new Car();
    
DataBase database1 = new DataBase();
    
car1.setCar();
     
database1.addCar(car1);
    
database1.printDataBase();
    }




PHP Code:
 import java.util.*;

class 
Car {

    private 
String _model;
    private 
String _color;
    private 
int _year;
    private 
Scanner _input = new Scanner(System.in);

    public 
Car() {

    
_model "empty";
    
_color "empty";
    
_year 0;

    }

    public 
void setCar() {

    
System.out.print("Enter model: ");
    
_model _input.next();
    
System.out.print("Enter color: ");
    
_color _input.next();
    
System.out.print("Enter year: ");
    
_year _input.nextInt();

    }

    public 
void formatCar() {
    
_model "empty";
    
_color "empty";
    
_year 0;
    }

    public 
String getModel() {
    
String model=_model;
    return 
model;
    }

    public 
String getColor() {
    
String color=_color;
    return 
color;
    }

    public 
int getYear() {
    
int year=_year;
    return 
year;
    }

    public 
void printCar() {
    
System.out.println("Model :"+_model);
    
System.out.println("Color :"+_color);
    
System.out.println("Year :"+_year);
    }





And the error i get:
PHP Code:
 yotam@main computer:~/Documents/java/newcarjava mainpage 
Exception in thread 
"main" java.lang.NullPointerException
    at DataBase
.<init>(DataBase.java:7)
    
at mainpage.main(mainpage.java:5)
yotam@main computer:~/Documents/java/newcar


i searched for possible reasons and i came across the possibility that the array wasn't initialized, but then i changed to constructor to set model and color to "empty" and year to 0.
so now i have no clue, need your help guys.
Thanks!

Reply With Quote
  #2  
Old September 22nd, 2010, 04:21 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
See the answering post on one of the other forums you posted this question on.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - Exception in thread "main" java.lang.NullPointerException

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap