Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-22-2010, 09:10 PM   PM User | #1
yotamoo
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
yotamoo is an unknown quantity at this point
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!
yotamoo is offline   Reply With Quote
Old 09-22-2010, 10:18 PM   PM User | #2
TheShaner
Senior Coder

 
TheShaner's Avatar
 
Join Date: Sep 2005
Location: Orlando, FL
Posts: 1,125
Thanks: 2
Thanked 40 Times in 40 Posts
TheShaner will become famous soon enoughTheShaner will become famous soon enough
It's because in this line:
Code:
private Car[] database = new Car[3];
You are creating a new array of Car, but when you do this:
Code:
for (int i=0; i<database.length; i++) {
    database[i].formatCar();
}
The code in red returns a null Car object because the Car instance has not been created yet for that position in the array. Then when it attempts to run the formatCar method, it returns a NullPointerException because you're trying to do the formatCar method on a null object. Before doing the line in red, you should do:
Code:
database[i] = new Car();
However, looking at your code, you should replace the array and use an ArrayList object instead like so:
Code:
List<Car> database = new ArrayList<Car>(3);
Then you won't have to create your own addCar method since an ArrayList already has that method implemented.

-Shane
TheShaner is offline   Reply With Quote
Users who have thanked TheShaner for this post:
yotamoo (09-23-2010)
Old 09-23-2010, 02:41 AM   PM User | #3
yotamoo
New to the CF scene

 
Join Date: Sep 2010
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
yotamoo is an unknown quantity at this point
i can't thank you enough, plus the dynamic array is just what i needed.
thanx!

also - can you pls link me to a tutorial explaining more about the list you told me about? i wasn't really sure what to look for.

p.s. can i mark this thread solved?

Last edited by yotamoo; 09-23-2010 at 02:57 AM..
yotamoo is offline   Reply With Quote
Old 04-24-2013, 06:36 PM   PM User | #4
paranrat
New to the CF scene

 
Join Date: Apr 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
paranrat is an unknown quantity at this point
Please help me! I got error.... I'm a new programming learner

import java.util.*;

class CustomerInfo{
String name, type;
int no;
double balance;
}

public class Customer_Account {
int N = 2;
CustomerInfo[] student = new CustomerInfo[N];

public static void main (String[] args)
{
int N = 2;
int i;
Customer_Account arr = new Customer_Account();
CustomerInfo[] Std = new CustomerInfo[N];
Std = arr.InputData();
arr.SelectOperation();
arr.PrintInfo(Std);

}
public CustomerInfo[] InputData() {
int i, num;
double amount, total;
Scanner input = new Scanner (System.in);
CustomerInfo[] customer = new CustomerInfo[N];
System.out.println("\nEnter Customer Information ");
System.out.println("___________________________ \n");
for (i = 0; i< N; i++)
{
customer[i] = new CustomerInfo();
System.out.print("Input customer name : ");
customer[i].name = input.next();
System.out.print("Input acct No : ");
customer[i].no = input.nextInt();
System.out.print("Input acct Type : ");
customer[i].type = input.next();
System.out.print("Input acct Balance : ");
customer[i].balance = input.nextDouble();
System.out.println();
}
return customer;
}

public void SelectOperation (){
int num, i=N;
double amount, total;
Scanner input = new Scanner (System.in);
CustomerInfo[] customer = new CustomerInfo[N];
Scanner choose = new Scanner (System.in);
System.out.println("Please choose your operation (input either 1 or 2): ");
System.out.println("1. Withdrawal");
System.out.println("2. Deposit");
System.out.println();
System.out.print("You choose: ");
num = choose.nextInt();
System.out.println();

switch (num) {
case 1:

System.out.println("Withdrawal Operation:");
System.out.println("___________________________ \n");
System.out.print("Input acct munber:");
customer[i].no = input.nextInt();
System.out.print("Input amount to withdraw:");
amount = input.nextInt();
total= (customer[i].balance-amount);
customer[i].balance = total;
System.out.println();
break;

case 2:
Scanner output = new Scanner (System.in);
System.out.println("Deposit Operation:");
System.out.println("___________________________ \n");
System.out.print("Input acct munber:");
customer[i].no = output.nextInt();
System.out.print("Input amount to deposit:");
amount = output.nextInt();
total= (amount+customer[i].balance);
customer[i].balance=total;
System.out.println();
break;
}
}

public void PrintInfo(CustomerInfo[] Std)
{
int i;
System.out.println();
System.out.println("List of customers :\n");
for (i=0;i<N;i++)
{
System.out.println("Customer "+(i+1) + "\n" +
"Customer Name : " + Std[i].name + "\n" +
"Customer Account Number : " + Std[i].no + "\n" +
"Customer Account Type :" + Std[i].type + "\n" +
"Customer Account Balnce : " + Std[i].balance + "\n");
}
}
}


The output:
Enter Customer Information
___________________________

Input customer name : Ali
Input acct No : 1111
Input acct Type : saving
Input acct Balance : 2222

Input customer name : Abu
Input acct No : 2222
Input acct Type : saving
Input acct Balance : 3333

Please choose your operation (input either 1 or 2):
1. Withdrawal
2. Deposit

You choose: 1

Withdrawal Operation:
___________________________

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at customer_account.Customer_Account.SelectOperation(Customer_Account.java:77)
at customer_account.Customer_Account.main(Customer_Account.java:30)
Input acct munber:Java Result: 1
paranrat is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:26 PM.


Advertisement
Log in to turn off these ads.