所以我试图将存储在一个类Liquor的ArrayList中的数据显示到另一个类Bar中。 ArrayList保存5个字符串(酒的名称)和5个整数(每种酒的量)。当我运行代码来显示信息时,我得到一个很长的错误:
线程“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) 在 javax.swing.AbstractButton中的$ Handler.actionPerformed(AbstractButton.java:2348) 在 javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) 在 javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) 在 javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) 在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) 在 java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) 在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 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) 在 java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) 在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) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:728)at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 在 java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在 java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82) BUILD STOPPED(总时间:12秒)
我的酒类:
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");
}
我认为这个问题与初始化我的Bar类中的private Liquor liquor;
有关,我已经尝试过不同的方法。但是地图册无法做到正确。任何帮助将不胜感激!
示例运行:
(barLocation)栏名为:(barName)
现场音乐:(真或假)
食品服务:(真或假)
现有的白酒:
伏特加:(库存量)威士忌:(库存量)
朗姆酒:(库存量)
杜松子酒:(库存量)白兰地:(库存量)
答案 0 :(得分:0)
据我所见,你从来没有初始化酒。
初始化看起来像这样
Liquor liquor = new Liquor("data", "data", ...);
我没有达到创造一个含有更多液体(伏特加等)的单一酒对象的目的。但也许你有理由。 如果没有,那么我会设计不同的酒,因为你所做的并不像现实。 通常情况下,酒类应该具有这样的属性:
public String name;
public double alcohol;
然后我会在你的栏中创建一个Liquors的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));
}
现在我的fill方法显然要求你的Liquid类有一个带2个参数的构造函数(String name,int alcohol)
答案 1 :(得分:0)
您尚未初始化Liquor对象。请尝试以下语句
private Liquor liquor=new Liquor("vodka",""...,1,2,..);