2016-10-15 9 views
0

私はコードを動作させる方法を理解しようとしていますが、nullErrorを取得し続けます。データを正しく保存していませんか?ここに私のコードは次のとおりです。Javaインベントリプロジェクト。配列

public class ProductTesterPart1{ 
public static void main(String[] args) { 
    Scanner userInput = new Scanner(System.in); 
    System.out.println("Please enter the product number, name, stock, and price in that order."); 

    List<ProductPart1> products = new ArrayList<ProductPart1>(); 
    String line = userInput.nextLine(); 
    while (!line.equals("quit")) { 
     if (line == null || line.trim().isEmpty()) { 
      products.add(new ProductPart1()); 
     } 
     else { 
      try { 
       Scanner s = new Scanner(line); 
       int number = s.nextInt(); 
       String name = s.next(); 
       int stock = s.nextInt(); 
       double price = s.nextDouble(); 

       products.add(new ProductPart1(number, name, stock, price)); 
      } 
      catch (NoSuchElementException e) { 
       System.out.print("Error: " + e.getMessage()); 
      } 
     } 
    } 

    for (ProductPart1 p : products) { 
      System.out.println(p.toString()); 
    } 
    } 

そして、もちろん、それはドライバーのクラスで、ここで私のオブジェクトは、次のとおりです。

public class ProductPart1 { 

//Declares variables 
private int productNumber; 
private String productName; 
private int productStock; 
private double productPrice; 

//Constructor 
public ProductPart1(){ 
    setNumber(0); 
    productName = "Null"; 
    productStock = 0; 
    productPrice = 0.0; 
} 

//Overload constructor 
public ProductPart1(int number, String name, int stock, double price){ 
    productNumber = number; 
    productName = name; 
    productStock = stock; 
    productPrice = price; 
} 

//set the number of the object 
public void setNumber(int newNumber){ 
    productNumber = newNumber; 
} 

//set the name of the object 
public void setName(String newName){ 
    productName = newName; 
} 

//set the stock of an object 
public void setStock(int newStock){ 
    productStock = newStock; 
} 

//set the price of an object 
public void setPrice(double newPrice){ 
    productPrice = newPrice; 
} 

//get the number of an object 
public int getNumber(){ 
    return getNumber(); 
} 

//get the name of an object 
public String getName(){ 
    return productName; 
} 

//get the stock of an object 
public int getStock(){ 
    return productStock; 
} 

//get the price of an object 
public double getPrice(){ 
    return productPrice; 
} 

//toString to bring the object together. 
public String toString(){ 
    return new String("Number: " + getNumber() + " Name: " + productName + " Price: " + productPrice + " Stock: " + productStock); 
} 
+0

を、あなたのコンソールログが表示されてくださいことはできますか? –

+0

これはいいです –

+0

nullError:nullError:nullError:nullError:nullError:nullError:nullError:nullError:nullError:nullError: –

答えて

3

は、この更新されたコード試してみてください。

public class ProductTesterPart1{ 
public static void main(String[] args) { 
    Scanner userInput = new Scanner(System.in); 
    System.out.println("Please enter the product number, name, stock, and price in that order."); 

    List<ProductPart1> products = new ArrayList<ProductPart1>(); 
    String line = userInput.nextLine(); 
    while (!line.equals("quit")) { 
     if (line == null || line.trim().isEmpty()) { 
      products.add(new ProductPart1()); 
     } 
     else { 
      try { 
       Scanner s = new Scanner(line); 
       int number = s.nextInt(); 
       String name = s.next(); 
       int stock = s.nextInt(); 
       double price = s.nextDouble(); 
       products.add(new ProductPart1(number, name, stock, price)); 
      } catch (NoSuchElementException e) { 
       System.out.print("Error: " + e.getMessage()); 
      } 
     } 
     if(userInput.hasNext()){ 
      line = userInput.nextLine(); 
     } else { 
      break; 
     } 
    } 

    for (ProductPart1 p : products) { 
      System.out.println(p.toString()); 
    } 
    } 
} 
+0

ありがとう!私の先生は、私がやっていたことよりも何かWAAYYYがほしいと思っています。オブジェクトを作成するために単純なコンストラクタを使用するだけで、配列やその他のものに格納する必要はありません。でもありがとう! –

+0

@トムありがとう:)。更新され、テストされたコードをご覧ください。 –

+0

はい、それは良いです:)。 Btwでは、 'line == null'チェックを削除することもできます。 'Scanner 'を使うときは" empty "をチェックすれば十分です。 – Tom

0

あなたのコードは無限ループを作成します。まず、製品番号、名前、在庫、および価格の行を読んでください。そして、あなたはこの行を読むwhileループに入りますが、再び行変数を変更することはないので、何度も何度も何度も読み込まれます。