Java program crashing when it overwrites an existing .txt file? - java

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!

Related

Creating JTextPane is leading to NullPointerException - OpenOffice Extension

I'm currently developing an Extension for OpenOffice. I'm using Java 1.6 and the OpenOffice SDK 4.1.2.
If I try to create a javax.swing.JTextPane, I get a NullpointerException in the Constructor of JTextPane.
public class Dialog extends javax.JFrame {
private final JTextPane jTextPane;
private final JTable jTable;
public Dialog() {
jTable = new JTable();
jTextPane = new JTextPane();
}
}
The Dialog is initialized in another Thread:
public class DialogManager {
private static JournalDialog journalDialog;
public void showDialog() {
Thread startThread = new Thread(new Runnable() {
#Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
dialog = new Dialog();
...
}
}
}
}
}}
The creation of JTable works fine, but in the next line I get a Nullpointerexception
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.util.Hashtable.put(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.registerEditorKitForContentType(Unknown Source)
at javax.swing.JEditorPane.loadDefaultKitsIfNecessary(Unknown Source)
at javax.swing.JEditorPane.getKitTypeRegistry(Unknown Source)
at javax.swing.JEditorPane.getEditorKitClassNameForContentType(Unknown Source)
at javax.swing.JTextPane.<init>(Unknown Source)
at .gui.Dialog.<init>(Dialog.java:159)
at .gui.DialogManager$6$1.run(DialogManager.java:334)
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)
I hope someone has an idea what causes this exception. I tried to run the extension in Java 1.7 and 1.8, but there is the same issue.
Best regards
Update 16.08.2016:
If I add a JTexPane over the Netbeans Palette into the Designer, it works. Only the initialisation in the constructor fails.
It may not be possible to use Swing for this task without crashing. Instead, use the com.sun.star.awt module. Complete examples are at http://api.libreoffice.org/examples/DevelopersGuide/examples.html#GraphicalUserInterfaces.
For more information, see Creating Dialogs at Runtime.
One more link: This example does use Swing. Try it to see if the same problem occurs.
My dirty solution is to initialize the JTextPane twice within a try-catch. Because on the second call i don't get an exception.

Why do I need to reset setText() in a JLabel to prevent errors?

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)

Simple Java Calculator Logic

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.

NullPointerException When trying to call Abstract class - Java

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();

Problems searching empty user database with ObjectDB

I am creating a java application which uses ObjectDB to create and maintain a set of databases. I'm currently trying to implement a DB to store user objects consisting of username and password strings. On a JFrame/swing class I have a button for creating new users, when this button is clicked, I want the following to happen:
Create (or connect to) database
Search the database to see if a user object exists with supplied username
if user already exists show a dialog message, otherwise create the user
However, when this button is clicked I get a 'User not found' error on the line which uses the results from the query object. I'm pretty sure it's because I have an empty database, however I want the code to work for the first time the program is ran, so it needs to handle an empty database. I've tried changing the code to create a new user before the query is first run, then the code works how I want it to each time the button is clicked, and it can detect whether or not a new user needs to be created.
I tried to create a 'default' or 'admin' type user so the search would work, however this means a repeat 'default' user will be created every time the program is ran, which is obviously an unwanted feature, and I can't do a query to check if the database is empty (so I could only create the default user on the first run of the program), because that is the problem I was having in the first place!
So, to anyone experienced with ObjectDB, is there a way I can handle the case where an empty database is searched?
Here's the relevant parts of my code:
public class FrameLogIn extends JFrame {
private JPanel contentPane;
private String username;
char[] password;
private static EntityManager em;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FrameLogIn frame = new FrameLogIn();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FrameLogIn() {
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("user.odb");
em = emf.createEntityManager();
final JTextField txtUsername = new JTextField();
txtUsername.setFont(new Font("Segoe UI Light", Font.PLAIN, 30));
txtUsername.setHorizontalAlignment(SwingConstants.CENTER);
txtUsername.setToolTipText("username");
txtUsername.setText("");
txtUsername.setBounds(220, 30, 177, 52);
contentPane.add(txtUsername);
txtUsername.setColumns(10);
final JPasswordField passwordField = new JPasswordField();
passwordField.setFont(new Font("Tahoma", Font.PLAIN, 30));
passwordField.setHorizontalAlignment(SwingConstants.CENTER);
passwordField.setBounds(220, 93, 177, 52);
contentPane.add(passwordField);
JButton btnNewUser = new JButton("New user");
btnNewUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
username = txtUsername.getText();
password = passwordField.getPassword();
System.out.println(username);
TypedQuery<Long> q = em.createQuery(
"SELECT COUNT(u) FROM User u "
+ "WHERE u.name = '" + username + "'", Long.class);
if (q.getSingleResult()>0) {
JOptionPane.showMessageDialog(contentPane.getParent(), "A user with this name already exists.");
} else {
em.getTransaction().begin();
User newUser = new User(username, password.toString());
em.persist(newUser);
em.getTransaction().commit();
JOptionPane.showMessageDialog(contentPane.getParent(), "User created.");
}
}
});
btnNewUser.setFont(new Font("Segoe WP Semibold", Font.PLAIN, 25));
btnNewUser.setBackground(Color.WHITE);
btnNewUser.setBounds(71, 175, 149, 63);
contentPane.add(btnNewUser);
}
}
and the error:
Exception in thread "AWT-EventQueue-0" [ObjectDB 2.5.3_03] SELECT COUNT(u) FROM ==> User <== u WHERE u.name = ''
javax.persistence.PersistenceException
Type User is not found (error 301)
(position 21) at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:723)
at sg.FrameLogIn$2.actionPerformed(FrameLogIn.java:113)
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)
Caused by: com.objectdb.o.TEX: Type User is not found
at com.objectdb.o.MSG.e(MSG.java:107)
at com.objectdb.o.TRS.g(TRS.java:212)
at com.objectdb.o.SYR.q(SYR.java:259)
at com.objectdb.o.SYR.n(SYR.java:188)
at com.objectdb.o.QRC.<init>(QRC.java:152)
at com.objectdb.o.QRM.U6(QRM.java:250)
at com.objectdb.o.MST.U6(MST.java:933)
at com.objectdb.o.WRA.U6(WRA.java:293)
at com.objectdb.o.WSM.U6(WSM.java:114)
at com.objectdb.o.QRR.g(QRR.java:245)
at com.objectdb.o.QRR.f(QRR.java:154)
at com.objectdb.jpa.JpaQuery.getSingleResult(JpaQuery.java:716)
... 37 more
Thanks, also for the record i will not be storing plaintext passwords, I'm just trying to get a basic DB working before I start implementing hashed passwords.
This could happen if the entity class is not in the database yet (no instances of that class have been persisted yet), and a persistence unit is not defined.
If this seems to be the cause, either define a persistence unit or introduce the class to ObjectDB before the query, for example by:
em.getMetamodel().entity(User.class);

Categories

Resources