值未从 class 转移到另一个 JAVA
Value is not transfered from a class to another JAVA
我有 idS 方面的问题。在 sellContainer 中 idS 为 1,在 sellCtr 中始终为 0,因此 addItemToSell 方法不起作用。总是给空指针异常。
public class SellCtr{
private SellContainer sellContainer;
private OrderLineSell ols;
private int idS;
private int idOls;
public SellCtr(){
sellContainer = SellContainer.getInstance();
}
public void createSell(String date, Customer c, Employee e){
Sell sell = new Sell(date,c,e);
sellContainer.addSell(sell);
idS = sell.getId();
}
public void addItemToSell(OrderLineSell orderLineSell){
sellContainer.findSell(idS).addOrderLineSell(orderLineSell);
}
public int getId(){
return idS;
}
public int getIdOls(){
return idOls;
}
public Sell findSell(int idS){
return sellContainer.findSell(idS);
}
}
另一个class是:
public class SellContainer{
private ArrayList<Sell> sales;
private static int idS=0;
private static SellContainer instance=null;
public SellContainer(){
sales = new ArrayList<>();
}
public static SellContainer getInstance(){
if(instance == null){
instance = new SellContainer();
}
return instance;
}
public void addSell(Sell s){
idS++;
s.setId(idS);
sales.add(s);
}
public Sell findSell(int idS){
for(Sell sell : sales){
if(sell.getId()==idS){
return sell;
}
}
return null;
}
}
这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controlLayer.SellCtr.addItemToSell(SellCtr.java:24)
at tuiLayer.SaleAddItemToSaleGUI.actionPerformed(SaleAddItemToSaleGUI.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我已经重建了项目并且异常发生了变化。
编辑 3:我已将 idS 设为静态,现在可以使用了。非常感谢你们!
我建议你
- 确保您正在编译所有 类。像 maven 或 gradle 这样的构建工具会有所帮助。
- 你对单例使用
enum
。这些是线程安全且更简单的。
- 禁用 eclipse 中允许您 运行 无法编译的代码的功能。这只会延迟发现错误,使它们变得更糟。
我会替换
public class SellContainer{
private ArrayList<Sell> sales;
private static int idS=0;
private static SellContainer instance=null;
public SellContainer(){
sales = new ArrayList<>();
}
public static SellContainer getInstance(){
if(instance == null){
instance = new SellContainer();
}
return instance;
}
与
public enum SellContainer {
INSTANCE;
private final List<Sell> sales = new ArrayList<>();
private int idS = 0;
在return对象返回被调用方法之前初始化对象。
在 SellContainer 中,如果条件失败导致将 Sell Object 返回为 NULL。
你需要修改这个逻辑。
return 空;
我有 idS 方面的问题。在 sellContainer 中 idS 为 1,在 sellCtr 中始终为 0,因此 addItemToSell 方法不起作用。总是给空指针异常。
public class SellCtr{
private SellContainer sellContainer;
private OrderLineSell ols;
private int idS;
private int idOls;
public SellCtr(){
sellContainer = SellContainer.getInstance();
}
public void createSell(String date, Customer c, Employee e){
Sell sell = new Sell(date,c,e);
sellContainer.addSell(sell);
idS = sell.getId();
}
public void addItemToSell(OrderLineSell orderLineSell){
sellContainer.findSell(idS).addOrderLineSell(orderLineSell);
}
public int getId(){
return idS;
}
public int getIdOls(){
return idOls;
}
public Sell findSell(int idS){
return sellContainer.findSell(idS);
}
}
另一个class是:
public class SellContainer{
private ArrayList<Sell> sales;
private static int idS=0;
private static SellContainer instance=null;
public SellContainer(){
sales = new ArrayList<>();
}
public static SellContainer getInstance(){
if(instance == null){
instance = new SellContainer();
}
return instance;
}
public void addSell(Sell s){
idS++;
s.setId(idS);
sales.add(s);
}
public Sell findSell(int idS){
for(Sell sell : sales){
if(sell.getId()==idS){
return sell;
}
}
return null;
}
}
这是错误:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at controlLayer.SellCtr.addItemToSell(SellCtr.java:24)
at tuiLayer.SaleAddItemToSaleGUI.actionPerformed(SaleAddItemToSaleGUI.java:93)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access0(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.awt.EventQueue.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
我已经重建了项目并且异常发生了变化。
编辑 3:我已将 idS 设为静态,现在可以使用了。非常感谢你们!
我建议你
- 确保您正在编译所有 类。像 maven 或 gradle 这样的构建工具会有所帮助。
- 你对单例使用
enum
。这些是线程安全且更简单的。 - 禁用 eclipse 中允许您 运行 无法编译的代码的功能。这只会延迟发现错误,使它们变得更糟。
我会替换
public class SellContainer{
private ArrayList<Sell> sales;
private static int idS=0;
private static SellContainer instance=null;
public SellContainer(){
sales = new ArrayList<>();
}
public static SellContainer getInstance(){
if(instance == null){
instance = new SellContainer();
}
return instance;
}
与
public enum SellContainer {
INSTANCE;
private final List<Sell> sales = new ArrayList<>();
private int idS = 0;
在return对象返回被调用方法之前初始化对象。 在 SellContainer 中,如果条件失败导致将 Sell Object 返回为 NULL。 你需要修改这个逻辑。
return 空;