单击我的提交按钮时,它会根据使用的字段验证输入字段,然后调用writeItemToFile()方法。
private void btnSubmit_actionPerformed(ActionEvent e)
{
if (validateInput()){
if (enabledFields == "music")
createMusicItem();
else
if (enabledFields == "product")
createProductItem();}
writeItemToFile();
}//btnSubmit_actionPerformed
writeItemToFile()方法很小,坦率地说可以跳过,但是我的作业需要它。它调用Inventory类中的write(Item项)方法。它沿着代码顶部的项目声明发送。
Item item;
问题是Item类是抽象的,所以当它被传递时它会给我一个NullPointerException。下面是我的写(项目项)方法,只是为了衡量。我的问题是,有没有办法实例化抽象类?我该如何避免这种方法。
public void write(Item item)
{
invFilename = item.getFileName();
File inventoryFile = new File(invFilename);
try
{
invWriter = new FileWriter(inventoryFile, true);
} // try
catch (IOException e)
{
System.out.println("ERROR: File " + invFilename + "could not opened: "
+ e.getMessage());
}//catch
try
{
invWriter.write(item.getFileRecord());
}//try
catch (IOException e)
{
System.out.println("ERROR: Product " + item.getFileRecord()
+ "could not be written to file " + invFilename + ": "
+ e.getMessage());
}//catch
}//write(Item item)
请保持温和。
错误讯息:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Inventory.write(Inventory.java:13)
at AddItemFrame.writeItemToFile(AddItemFrame.java:575)
at AddItemFrame.btnSubmit_actionPerformed(AddItemFrame.java:500)
at AddItemFrame.access$1(AddItemFrame.java:492)
at AddItemFrame$3.actionPerformed(AddItemFrame.java:217)
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.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.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)
答案 0 :(得分:1)
由NullPointerException
方法触发的write
可能是由null
Item
类型的item.getFileName();
参数引起的,具体在:
null
- >调用Object
new File(invFilename)
的方法。
也可能是由于:
造成的 File
- >使用null
参数初始化新item
(如果null
不是item.getFileName()
,但Item
是。)
......等等。
与String
是抽象类的事实无关。
注意强>
正如评论中其他人所述,equals
内容比较由==
完成,而不是{{1}}运算符。
答案 1 :(得分:0)
答案 2 :(得分:0)
“问题是Item类是抽象的,所以当它被传递时它会给我一个NullPointerException。”
摘要并不意味着null
。抽象类不能直接实例化,但它的子类可以。因此,拥有诸如
public String getAbstrClassesToString(AnAbstractClass abstract_class) {
try {
return abstract_class.toString();
} catch(NullPointerException npx) {
if(abstract_class == null) {
throw new NullPointerException("abstract_class");
}
//Just in case the toString() throws an NPX
throw npx;
}
}
现在你可以用
来调用它getAbstrClassesToString(null);
这显然会产生NullPointerException
, 或 ,你可以用
getAbstrClassesToString(new ConcreteSubClassOfAnAbstractClass());
这是完全合法的,显然不 null。
换句话说,您的NullPointerException
问题与抽象无关。
更多信息:https://www.google.com/search?q=abstract+class+java+tutorials
答案 3 :(得分:-5)
必须实例化要传递的Item
类型的对象。
正如其他人所指出的,你不能简单地通过以下方式创建它:
Item item = new Item(); // this does not work!
但是,您可以extend
该类并创建所需的所有抽象方法,然后实例化新类。
class ItemB extends Item
{
...
}
然后你可以使用新类
ItemB item = new ItemB();