从一个类的ArrayList中获取数据到另一个
所以我试图将存储在一个类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)
嘛,据我可以看到你永远不会初始化酒。
初始化是这样的
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个参数的构造函数(字符串名称,诠释酒精)
您还没有初始化白酒object.Try下面的语句
private Liquor liquor=new Liquor("vodka",""...,1,2,..);
你是对的 - 你没有初始化酒属性,但试图调用它的方法 –
用'bars.get(i)替换'bars.get(i).getLiquor()。toString()' ).getLiquor()'摆脱异常,但这不会解决您的逻辑问题 –
你也会对ArrayList的结果感到失望#toString – MadProgrammer