When my submit button is clicked it validates input fields depending on which fields were used, then calls the writeItemToFile() method.
private void btnSubmit_actionPerformed(ActionEvent e)
{
if (validateInput()){
if (enabledFields == "music")
createMusicItem();
else
if (enabledFields == "product")
createProductItem();}
writeItemToFile();
}//btnSubmit_actionPerformed
The writeItemToFile() method is small and frankly could be skipped, but is needed for my assignment. It calls the write(Item item) method in my Inventory class. It sends along the item declaration at the top of my code.
Item item;
The problem is that the Item class is abstract so when it's passed it gives me a NullPointerException. Below is my write(Item item) method just for good measure. My question is this, is there a way I have to instantiate the abstract class? How do I avoid this method.
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)
Please be gentle.
Error message:
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)
A NullPointerException triggered by your write method is likely to be caused by a null argument of type Item, specifically on:
item.getFileName(); --> invoking method of null Object.
It could also be caused by:
new File(invFilename) --> initializing new File with null argument (if item is not null, but item.getFileName() is).
... etc.
Nothing to do with the fact that Item is an abstract class.
Note
As mentioned by others in the comments, String contents comparison is done by equals, not the == operator.
the Item class is abstract
That means you cannot have an object of this class. You need to extend the abstract class and pass an object of your new class.
Check here : Abstract classes cannot be instantiated, but they can be subclassed
"The problem is that the Item class is abstract so when it's passed it gives me a NullPointerException."
Abstract does not mean null. An abstract class cannot be directly instantiated, but its sub-classes may. Therefore it is perfectly legal to have a function such as
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;
}
}
Now you could call this with
getAbstrClassesToString(null);
which will obviously result in a NullPointerException, or you could call this with
getAbstrClassesToString(new ConcreteSubClassOfAnAbstractClass());
which is perfectly legal, and obviously not null.
In other words, your NullPointerException problem is unrelated to abstract-ness.
More information: https://www.google.com/search?q=abstract+class+java+tutorials
The object of type Item that is being passed must be instantiated.
As pointed out by others, you cannot simply create it via:
Item item = new Item(); // this does not work!
However, you can extend the class and create all the abstract methopds required and then instantiate the new class.
class ItemB extends Item
{
...
}
Then you can work with the new class,
ItemB item = new ItemB();
Related
I create a button to copy the selected pixel color.
It works fine if I click the picture(get the pixel value) first, then click the "copy" button. But if I click the "copy" button first, I will receive this error.
Any thoughts?
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Pixel value is null
at a9.ImageEditorController.copy(ImageEditorController.java:81)
at a9.PixelInspectorTool.actionPerformed(PixelInspectorTool.java:95)
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$500(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$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.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$JavaSecurityAccessImpl.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)
public void copy() {
Pixel selectedB = model.getSelected();
if (selectedB == null) {
throw new IllegalArgumentException("cannot be null"); //line 81
}
PaintBrushToolUI pbtUI = (PaintBrushToolUI) paint_brush_tool.getUI();
pbtUI.changeView(selectedB);
}
public void actionPerformed(ActionEvent arg0) {
for (ToolButtonListener t : tList) {
t.copy(); // this is line 95
System.out.println("Copying");
}
}
Well actually you get this error message because You throw it exactly here:
if (selectedB == null) {
// code bellow throws your exception...
throw new IllegalArgumentException("cannot be null"); //line 81
}
To solve your problem you can check if your model is null here:
for(ToolButtonListener t:tList) {
//check here if it's null for example
if(model.getSelected() != null){
t.copy(); // this is line 95
System.out.println("Copying");
}
}
Or you can make the copy method to check if object is null like this:
public void copy() {
Pixel selectedB= model.getSelected();
if (selectedB != null) {
// throw new IllegalArgumentException("cannot be null"); //line 81
PaintBrushToolUI pbtUI = (PaintBrushToolUI)paint_brush_tool.getUI();
pbtUI.changeView(selectedB);
}
}
This error occurs because before you click that other button to create color the first one(copy button) doesn't have "source" to copy from.
You can also make the copy button inactive until you click the other button. There are various solutions for your problem(probably a lot better than mine) and it all depends on what effect you want to achieve.
As a follow-up to this question that I posted earlier, I am wondering about the cause of the issue I had.
The problem was that I was getting this error when updating a JLabel with a lot of HTML text.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.text.html.StyleSheet$ListPainter.paint(Unknown Source)
at javax.swing.text.html.ListView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.text.html.ListView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.text.BoxView.paintChild(Unknown Source)
at javax.swing.text.BoxView.paint(Unknown Source)
at javax.swing.text.html.BlockView.paint(Unknown Source)
at javax.swing.plaf.basic.BasicHTML$Renderer.paint(Unknown Source)
at javax.swing.plaf.basic.BasicLabelUI.paint(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(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$JavaSecurityAccessImpl.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)
The HTML that's being set is generated by a StringBuilder and the converted .toString. Something like this, but more extensive:
public static String entityOverviewHTML() {
StringBuilder sb = new StringBuilder();
List<Entity> entities = Entity.getEntities();
sb.append("<html><div style='padding: 12px;'>");
sb.append("<h1>People: alive</h1>");
// A lot of appending: also loop through getEntities (dynamic, can change)
// and get contents from each entity in #getEntityInfo below
if (entities.size() > 0) {
sb.append("<ul>");
for (Entity e: entities) {
getEntityInfo(e);
}
sb.append("</ul>");
}
sb.append("</div></html>");
return sb.toString();
}
private static StringBuilder getEntityInfo(Entity e) {
StringBuilder sbInfo = new StringBuilder();
// A lot of appending: append info of Entity e such as e.getFirstName()
sbInfo.append("<li>");
sbInfo.append(e.getFirstName())
sbInfo.append("</li>");
return sbInfo;
}
After some events, the HTML will change after which I call a custom refresh method:
public static void bringToFront() {
getInstance().setVisible(true);
getInstance().setExtendedState(JFrame.ICONIFIED);
getInstance().setExtendedState(JFrame.NORMAL);
}
public static void refresh() {
// text is a JLabel
text.setText(entityOverviewHTML());
bringToFront();
}
And it's then that the errors at the top of this post happen, however not always! I haven't figured out why this happens, but I did find that when resetting the text to an empty string and then calling entityOverviewHTML solves the issue.
public static void refresh() {
text.setText(""); // Here we go
text.setText(entityOverviewHTML());
bringToFront();
}
text is defined as a Class variable:
private static JLabel text = new JLabel();
What I like to know is: why? How can this single line of seemingly obsolete code solve the problem? Exactly what is the problem?
The problem is that your refresh() method is not called on the Swing EDT.
Swing is not threadsafe (https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html) which means that the call text.setText(entityOverviewHTML()); corrupts the internal data structures that your JLabel instance uses to display the text.
Your refresh-method needs to be rewritten like this:
public static void refresh() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
text.setText(entityOverviewHTML());
bringToFront();
}
});
} catch (InvocationTargetException | InterruptedException e) {
e.printStackTrace();
}
}
Usually when JLabel is instantiated, it has no width , if it is instantiated with empty text width is zero. When JLabel text is updated it won't show up as its size is not set properly.
Use preferred size: text.setPreferredSize(new Dimension(x, y)); then text.setText(html)
I'm new to programming and I was making a calculator with only 1 textfield.
I need a method which can recognize which string characters from these (+ , - , * , /)
to send the result to a variable and when i click the = button it shows the result.
I have tried to write something like that (1+2) and save it to variable then then when I try to press = button to settext the variable it shows a privilage error.
Here is the code
JButton btnOne = new JButton("1");
btnOne.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sum+=1;
txtoprtn.setText(txtoprtn.getText()+"1");
JButton btnTwo = new JButton("2");
btnTwo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
sum+=2;
txtoprtn.setText(txtoprtn.getText()+"2");
}
});
JButton btnAdd = new JButton("+");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
txtoprtn.setText(txtoprtn.getText()+"+");
}
});
JButton btnEqual = new JButton("=");
btnEqual.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
x = Integer.parseInt(txtoprtn.getText());
txtoprtn.setText(Integer.toString(x));
}
}
);
and this is the error
**Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1+2"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.jadv.day01.tasks.AdvCalc$11.actionPerformed(AdvCalc.java:140)
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$500(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)**
any suggestions????
First problem
When you try to just write a string statement of "1+2", what do you expect will happen? It can't simply just be evaluated like that. You would have to first use Integer.parseInt() to each of your # buttons after the number insertion is done(say, when you click the +/-///*, or =. That means the number is done and you'll get say
Integer.parseInt("123") instead of Integer.parseInt("1")+Integer.parseInt("2")+Integer.parseInt("3")
What you're trying to do is parsing a + in the middle of the parse. Keep the integer parse to the numbers and the operators stored somewhere else(explained in second problem)
Second problem
When you say "+", that doesn't parse into any operation. You would have to store the values into their individual variables and when clicked a + button, you would get a correct answer. If you want to do this without any external imports, then you would have to store the operations in a list and evaluate when = is clicked.
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
operations.add("+");//operations is a list
}
});
Solving both problems at once using an import
If you want to evaluate using a package, then use this:
ScriptEngine evaluationMachine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.eval(foo); //evaluates something like "2+1" into 3.
with the following imports:
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
EDIT: By the way, though the second way is much faster, you should probably try and write it without external imports since you're new to programming. There will be inevitable situations where you're going to have to think about the problem.
The cause of the exception is that you are trying to parse a String like "1+2" to an Integer in the line:
x = Integer.parseInt(txtoprtn.getText());
You should store the numbers somewhere else, i.e. in an ArrayList.
I have created a jar of my project on Speech Recognition in JAVA through Sphinx. My code is executing perfectly but when I am creating its jar through "runnable jar file->copy required libraries into sub folder", and executing it through cmd with command " java -jar {jar name}.jar" it opens but after selecting the speak button or invoking sphinx method it gives error edu.cmu.sphinx.jsapi.JSGFGrammar class not found.
I am not getting any way how to resolve this.
my speech to text code is:
package com.ongraph;
import edu.cmu.sphinx.frontend.util.Microphone;
import edu.cmu.sphinx.recognizer.Recognizer;
import edu.cmu.sphinx.result.Result;
import edu.cmu.sphinx.util.props.ConfigurationManager;
public class SpeechToTextOperation {
ConfigurationManager cm;
SpeechRecognizer speechRecognizer;
Result result;
Recognizer recognizer;
Microphone microphone;
private final static String STOP = "stop";
private final static String XML_FILE = "helloworld.config.xml";
public void voiceGet() throws InterruptedException {
String resultString = null;
int count_Check = 0;
if (cm == null) {
cm = new ConfigurationManager(getClass().getClassLoader().getResource(XML_FILE));
}
if (recognizer == null) {
recognizer = (Recognizer) cm.lookup("recognizer");
microphone = (Microphone) cm.lookup("microphone");
microphone.clear();
}
recognizer.allocate();
if (!(microphone.startRecording())) {
System.out.println("Cannot start microphone.");
recognizer.deallocate();
System.exit(1);
}
instructions();
//SpeechRecognizer.please_Speak.setVisible(true);
while (true) {
System.out
.println("Start speaking. Speak 'Stop' to Stop Recording.");
if(count_Check == 0)
{
SpeechRecognizer.textArea.append("\n Start speaking...\n");
count_Check++;
}
Result result = recognizer.recognize();
resultString = result.getBestFinalResultNoFiller();
if (resultString != null && !"".equals(resultString)
&& !resultString.contains(STOP)) {
SpeechRecognizer.textArea.append(resultString + "\n");
} else {
SpeechRecognizer.textArea
.append("'Application Stopped. Press 'Speak' again to restart'");
recognizer.deallocate();
microphone.stopRecording();
break;
}
}
}
public void voiceStop() {
microphone.clear();
cm = null;
}
public void instructions() {
// TODO Auto-generated method stub
SpeechRecognizer.please_Speak.setVisible(true);
}
}
errors in cmd are:
class not found !java.lang.ClassNotFoundException: edu.cmu.sphinx.jsapi.JSGFGrammar
Exception in thread "AWT-EventQueue-0" Property Exception component:'flatLinguist' property:'grammar' - mandatory property is not set!
edu.cmu.sphinx.util.props.InternalConfigurationException
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:291)
at edu.cmu.sphinx.linguist.flat.FlatLinguist.newProperties(FlatLinguist.java:246)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
at edu.cmu.sphinx.decoder.search.SimpleBreadthFirstSearchManager.newProperties(SimpleBreadthFirstSearchManager.java:179)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
at edu.cmu.sphinx.decoder.AbstractDecoder.newProperties(AbstractDecoder.java:65)
at edu.cmu.sphinx.decoder.Decoder.newProperties(Decoder.java:37)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
at edu.cmu.sphinx.util.props.PropertySheet.getComponent(PropertySheet.java:279)
at edu.cmu.sphinx.recognizer.Recognizer.newProperties(Recognizer.java:90)
at edu.cmu.sphinx.util.props.PropertySheet.getOwner(PropertySheet.java:460)
at edu.cmu.sphinx.util.props.ConfigurationManager.lookup(ConfigurationManager.java:161)
at com.ongraph.SpeechToTextOperation.voiceGet(SpeechToTextOperation.java:24)
at com.ongraph.SpeechRecognizer$1.actionPerformed(SpeechRecognizer.java:50)
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$400(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(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.AccessControlContext$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)
Overall it's better to use latest version 5prealpha and the API described at http://cmusphinx.sourceforge.net/wiki/tutorialsphinx4
As for your exception, it says that class is not found. You need to pack that class into jar in order to run your code. There could be also a difference in package name. Recently edu.cmu.sphinx.jsapi package was renamed to edu.cmu.sphinx.jsgf. You might have issues to update that.
I'm making a simple text based game where you have to type commands to preform certain actions. I recently added a feature to the game that allows you to save your progress. But for some reason if you try to save your game over an existing save file it crashes. Here is the code that saves the game (when it fails to save it says "There was an error when trying to save game data. The game will now close." like expected):
import java.util.Formatter;
import javax.swing.JOptionPane;
public class Gamesave {
private static Formatter gamesave;
private static Formatter firstTimeSave;
private static Formatter attackpoints;
private static Formatter defensepoints;
private static Formatter skillpoints;
private static Formatter wins;
private static Formatter loses;
private static Formatter money;
// Attackpoints, defensepoints, skillpoints, wins, loses, money
public static void openFile(){
try{
attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
}catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}
}
public static void addRecords(){
attackpoints.format("%s",MainClass.attackpoints);
defensepoints.format("%s",MainClass.defensepoints);
skillpoints.format("%s",MainClass.skillpoints);
wins.format("%s",MainClass.wins);
loses.format("%s",MainClass.loses);
money.format("%s",MainClass.money);
firstTimeSave.format("%b", MainClass.firstTime);
}
public void closeFile(){
attackpoints.close();
defensepoints.close();
skillpoints.close();
wins.close();
loses.close();
money.close();
gamesave.close();
firstTimeSave.close();
}
}
Here is the code that calls the classes:
static class SaveAction implements ActionListener{
public void actionPerformed (ActionEvent e){
try{
Gamesave.openFile();
Gamesave.addRecords();
save.closeFile();
JOptionPane.showMessageDialog(null, "Your game has been saved.");
}catch (Exception e1) {JOptionPane.showMessageDialog(null, "Sorry, that is an invalid response.");}
}
}
Another note, when the game is launched for the first time on a computer it creates the directories for the save files and anything else needed. Thank you for any help!
The stack trace:
java.io.FileNotFoundException: c:\FightNight\Saves\null\null_attackpoints.txt (The system cannot find the path specified)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.io.FileOutputStream.<init>(Unknown Source)
at java.util.Formatter.<init>(Unknown Source)
at Gamesave.openFile(Gamesave.java:16)
at CommandLine$SaveAction.actionPerformed(CommandLine.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.access$000(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)
It seems all your GameSave methods are static, save for the closeFile method. However all reference the same fields. Try make closeFile static. It seems to reference static fields after all.
And of course, call it like the other methods, as in: GameSave.closeFile, not someInstanceOfGameSave.closeFile.
Finally, if that doesn't work, add the line e.printStackTrace(); before showing your message dialog and print the resulting stack trace as an edit of your question.
Edit
Make sure you check for nulls in your closeFile method as well.
It is because you have nulls in your file path. Notice the first line of your error stack
java.io.FileNotFoundException: c:\FightNight\Saves\**null\null**_attackpoints.txt (The system cannot find the path specified)
There is no possible way that you can have a null in the file path name. So, with that being said, you need to go back and fix the objects that are containing the information in this part of the code. Also notice this:
at Gamesave.openFile(Gamesave.java:16)
This line tells you exactly where the error is..
So, lets check that method...
try{
attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
}catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}
I'm not seeing a way that this class accesses the static class "MainClass"'s newProfileName variable....so there is most likely the reason as to why you are not getting the proper information in the file name..
I'd say to update the newProfileName inside the try/catch block, to keep it up to date...
something like so
try{
MainClass.newProfileName = //accessed information from whereever you get your new profile name in the Code....
attackpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_attackpoints.txt");
defensepoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_defensepoints.txt");
skillpoints = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_skillpoints.txt");
wins = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_wins.txt");
loses = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_loses.txt");
money = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+"_money.txt");
gamesave = new Formatter("c:\\FightNight\\Saves\\"+MainClass.newProfileName+"\\"+MainClass.newProfileName+".txt");
firstTimeSave = new Formatter("c:\\FightNight\\Game Data\\firstTimeSave.txt");
}catch (Exception e) {JOptionPane.showMessageDialog(null, "There was an error when trying to save game data. The game will now close."); System.exit(0);}
The system is getting a null in the file name, which cannot be. Null means it is nothing, an d has no directory position...so find a way to update that variable, and that should fix it...
Hope this helps!