2016-03-07 90 views
0

所以我試圖將存儲在一個類Liquor的ArrayList中的數據顯示到另一個類Bar中。 ArrayList包含5個字符串(酒的名稱)和5個整數(每種酒的數量)。當我運行的代碼,以顯示信息,我得到一個非常長的錯誤:從一個類的ArrayList中獲取數據到另一個

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at barinventory.BarInv.jButtonDisplayActionPerformed(BarInv.java:355) at barinventory.BarInv.access$200(BarInv.java:8) at barinventory.BarInv$3.actionPerformed(BarInv.java:219) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6535) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6300) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4891) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) BUILD STOPPED (total time: 12 seconds)

我的白酒類:

final class Liquor 
{ 
    //String[] liquor = {"Vodka", "Whiskey", "Rum", "Gin", "Brandy"}; 
    private final String vodka, whiskey, rum, gin, brandy; 
    private final int vCount, wCount, rCount, gCount, bCount; 
    ArrayList<Liquor> liquors = new ArrayList<>(); 

// public Liquor(ArrayList<Liquor> liquors) 
// { 
//   
//  this.vodka = "Vodka"; 
//  this.whiskey = "Whiskey"; 
//  this.rum = "Rum"; 
//  this.gin = "Gin"; 
//  this.brandy = "Brandy"; 
//  this.vCount = getVC(); 
//  this.wCount = getWC(); 
//  this.rCount = getRC(); 
//  this.gCount = getGC(); 
//  this.bCount = getBC(); 
// } was trying to pass it through the ArrayList itself... Didn't work. 
     //The stock number kept being read in as 0. 



    public Liquor(String vodka, String whiskey, String rum, String gin, 
      String brandy, int v, int w, int r, int g, int b) 
    { 
     this.vodka = vodka; 
     this.whiskey = whiskey; 
     this.rum = rum; 
     this.gin = gin; 
     this.brandy = brandy; 
     this.vCount = v; 
     this.wCount = w; 
     this.rCount = r; 
     this.gCount = g; 
     this.bCount = b; 
    } 

    public String getV() 
    { 
     return vodka; 
    } 

    public String getW() 
    { 
     return whiskey; 
    } 

    public String getR() 
    { 
     return rum; 
    } 

    public String getG() 
    { 
     return gin; 
    } 

    public String getB() 
    { 
     return brandy; 
    } 

    public int getVC() 
    { 
     return vCount; 
    } 

    public int getWC() 
    { 
     return wCount; 
    } 

    public int getRC() 
    { 
     return rCount; 
    } 

    public int getGC() 
    { 
     return gCount; 
    } 

    public int getBC() 
    { 
     return bCount; 
    } 

    @Override 
    public String toString() 
    { 
     return "\nLiquor currently in stock:\n" + vodka + ": " + vCount + "\n" + 
       whiskey + ": " + wCount + "\n" + rum + ": " + rCount + "\n" + 
       gin + ": " + gCount + "\n" + brandy + ": " + bCount; 
    } 
} 

我的酒吧類:

class Bar 
{ 
    private final String barLoc, barName; 
    private final boolean music, food; 
    //ArrayList<Liquor> liquor; 

    public Bar(String l, String n, boolean m, boolean f) 
    { 
     this.barLoc = l; 
     this.barName = n; 
     this.music = m; 
     this.food = f; 
    } 

    //ArrayList<Liquor> liquor = new ArrayList<>(); 
    private Liquor liquor; 

    public Liquor getLiquor() 
    { 
     return liquor; 
    } 

    @Override 
    public String toString() 
    { 
     return "The " + barLoc + " bar is named: " + barName + "\nLive music: " 
       + music + "\nFood Service: " + food; 
    } 
} 

代碼用於顯示:

for(int i=0; i<bars.size(); i++) 
     { 
      jTextAreaDisplay.append(jTextAreaDisplay.getText() 
        + bars.get(i).toString() + bars.get(i).getLiquor().toString() 
        + "\n\n"); 

     } 

我認爲問題em必須在我的Bar類中初始化private Liquor liquor;,我嘗試過不同的方式。但阿特拉斯不能正確的。任何幫助將不勝感激!

運行示例:

The (barLocation) bar is named: (barName)

Live music: (true or false)

Food Service: (true or false)

Liquor currently in stock:

Vodka: (amount of stock)

Whiskey: (amount of stock)

Rum: (amount of stock)

Gin: (amount of stock)

Brandy: (amount of stock)

+0

你是對的 - 你沒有初始化酒屬性,但試圖調用它的方法 –

+0

用'bars.get(i)替換'bars.get(i).getLiquor()。toString()' ).getLiquor()'擺脫異常,但這不會解決您的邏輯問題 –

+0

你也會對ArrayList的結果感到失望#toString – MadProgrammer

回答

0

嘛,據我可以看到你永遠不會初始化酒。

初始化是這樣的

Liquor liquor = new Liquor("data", "data", ...); 

我不獲取創建一個單一的白酒對象,它包含更多的液體(伏特加酒等)的目的。但是,也許你有你的理由。 如果沒有,那麼我會設計不同的白酒,因爲你所做的並不真實。 通常情況下,類白酒應有性能的地方是這樣的:

public String name; 
public double alcohol; 

然後,我會在你的酒吧創造白酒的ArrayList和一些值填充它。

ArrayList<Liquor> liquors = new ArrayList<Liquor>(); 
//Fill with something like 4 liquors i call them vodka 0, 1, 2 and 3 and fill them with the alcohol value of 40 
for(int i = 0; i < 4; i++){ 
    liquors.add(new Liquor("Wodka"+i, 40)); 
} 

現在我的填充方法顯然需要您的液體類有2個參數的構造函數(字符串名稱,詮釋酒精)

0

您還沒有初始化白酒object.Try下面的語句

private Liquor liquor=new Liquor("vodka",""...,1,2,..); 
相關問題