Applet runs in eclipse but not in browser - java security - java

Below is applet code, it uses jna.jar (https://github.com/twall/jna) to access a DLL file in system32.
import javax.swing.*;
import javax.print.*;
import java.security.*;
import java.util.ArrayList;
import com.sun.jna.Library;
import com.sun.jna.Native;
import java.awt.*;
import java.awt.event.*;
import java.io.PrintWriter;
import java.io.StringWriter;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.WNDENUMPROC;
import com.sun.jna.win32.StdCallLibrary;
public class CallApplet extends JApplet implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 4654357272074276081L;
static JTextField output;
private final String ButtonText = "Print";
public void init() {
Container contentHolder = getContentPane();
contentHolder.setLayout(new BorderLayout(18,18));
output = new JTextField(20);
//add(output);
contentHolder.add(output, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton b = new JButton(ButtonText);
b.addActionListener(this);
buttonPanel.add(b);
contentHolder.add(buttonPanel, BorderLayout.SOUTH);
validate();
}
public void actionPerformed(ActionEvent evt)
{
//get the text of the button that was pushed
String command = evt.getActionCommand();
output.setText(command);
// if myButton was pressed, output a message
if(ButtonText.equals(command)) {
try {
this.mprintt("TSC TTP-244 Plus");
} catch (Exception e) {
}
}
}
public void setMessage(String message) {
output.setText(message);
}
public interface TscLibDll extends Library {
TscLibDll INSTANCE = (TscLibDll) Native.loadLibrary ("TSCLIB", TscLibDll.class);
int about ();
int openport (String pirnterName);
int closeport ();
int sendcommand (String printerCommand);
int setup (String width,String height,String speed,String density,String sensor,String vertical,String offset);
int downloadpcx (String filename,String image_name);
int barcode (String x,String y,String type,String height,String readable,String rotation,String narrow,String wide,String code);
int printerfont (String x,String y,String fonttype,String rotation,String xmul,String ymul,String text);
int clearbuffer ();
int printlabel (String set, String copy);
int formfeed ();
int nobackfeed ();
int windowsfont (int x, int y, int fontheight, int rotation, int fontstyle, int fontunderline, String szFaceName, String content);
}
public void mprintt(String printer) {
try {
//TscLibDll.INSTANCE.about();
TscLibDll.INSTANCE.openport(printer);
//TscLibDll.INSTANCE.downloadpcx("C:\\UL.PCX", "UL.PCX");
TscLibDll.INSTANCE.sendcommand("REM ***** This is a test by JAVA. *****");
TscLibDll.INSTANCE.setup("35", "15", "3", "8", "0", "3", "-1");
TscLibDll.INSTANCE.clearbuffer();
//TscLibDll.INSTANCE.sendcommand("PUTPCX 550,10,\"UL.PCX\"");
TscLibDll.INSTANCE.printerfont ("290", "8", "3", "0", "1", "1", "ARTICLE NO");
TscLibDll.INSTANCE.barcode("290", "35", "128", "50", "1", "0", "2", "2", "123456789");
//TscLibDll.INSTANCE.windowsfont(400, 200, 48, 0, 3, 1, "arial", "DEG 0");
//TscLibDll.INSTANCE.windowsfont(400, 200, 48, 90, 3, 1, "arial", "DEG 90");
//TscLibDll.INSTANCE.windowsfont(400, 200, 48, 180, 3, 1, "arial", "DEG 180");
//TscLibDll.INSTANCE.windowsfont(400, 200, 48, 270, 3, 1, "arial", "DEG 270");
TscLibDll.INSTANCE.printlabel("1", "1");
TscLibDll.INSTANCE.closeport();
output.setText("printed");
} catch (Exception e) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
output.setText(sw.toString());
}
}
}
My Html code
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<applet codebase ="." code="CallApplet.class"
archive="CallApplet.jar,jna.jar,platform.jar"
height="100" width="100"/>
</body>
</html>
I can see the applet loaded and all swing elements in browser when i try to click print it just wont do anything. In eclipse run it works fine.
I know this is security issue so i have also added policy (C:\Program Files\Java\jre7\lib\security\java.policy file)
grant codeBase "http://localhost:8080/appletproj/*" {
permission java.security.AllPermission;
};
Stacktrace
Exception in thread "AWT-EventQueue-7" java.lang.NoClassDefFoundError: com/sun/jna/Library
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.defineClassHelper(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.access$100(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin2.applet.Plugin2ClassLoader.findClassHelper(Unknown Source)
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at CallApplet.mprintt(CallApplet.java:185)
at CallApplet.actionPerformed(CallApplet.java:60)
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.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: java.lang.ClassNotFoundException: com.sun.jna.Library
at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 55 more
But even this does not help. please guide me where am I going wrong.
SOLUTION
Frankly i did not got solution to work, rather it was not a security issue so creating new question with actual error
So to get the exact error i enabled java console (suggested by andrew), you can go here for how to do it.
UPDATE
After few days I got it working, the issue was jna.jar and platform.jar which were already signed so whenever i signed them using jarsigner it never worked and i never got any error (so i never looked at them), when i verified it using jarsigner i figured it out. Then i unsigned them and signed it using same key which i used with my jar (Hope it will help others)

..also added policy .. But even this does not help.
Policy files are rarely, if ever, a good idea. If the code needs to be trusted, digitally sign the archive(s).

Related

Why am I getting an ArrayIndexOutOfBoundsException when I delete JTable row?

I am getting .ArrayIndexOutOfBoundsException when i try to delete a row from my JTable.
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Vector.java:734)
at java.util.Vector.elementAt(Vector.java:477)
at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:648)
at Test$1.valueChanged(Test.java:34)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:184)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:164)
at javax.swing.DefaultListSelectionModel.fireValueChanged(DefaultListSelectionModel.java:211)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(DefaultListSelectionModel.java:677)
at javax.swing.JTable.tableRowsDeleted(JTable.java:4509)
at javax.swing.JTable.tableChanged(JTable.java:4412)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:296)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:261)
at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:463)
at Test$2.actionPerformed(Test.java:39)
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)
First, I select the row I want to delete and then when I fire the action the delete button it freezes a bit and gives me exception.
Here is my complete code:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
public class Test extends JFrame{
private static JTable table;
private DefaultTableModel model = new DefaultTableModel();;
private JButton del = new JButton("DELETE");
public Test(){
JPanel pan= new JPanel();
JTextField numeField = new JTextField(30);
model.addColumn("Id");
model.addColumn("Nume");
table = new JTable(model);
JScrollPane pen = new JScrollPane(table);
del.setEnabled(false);
pan.add(pen);
pan.add(numeField);
pan.add(del);
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
del.setEnabled(true);
numeField.setText(model.getValueAt(table.getSelectedRow(), 1).toString());
}
});
del.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
model.removeRow(table.getSelectedRow());
}
});
Object[] row = {"1","NAME"};
model.addRow(row);
add(pan);
setExtendedState(MAXIMIZED_BOTH);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) throws IOException {
new Test();
}
}
Full stack trace :
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.Vector.elementData(Unknown Source)
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at Test$1.valueChanged(Test.java:34)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueChanged(Unknown Source)
at javax.swing.DefaultListSelectionModel.removeIndexInterval(Unknown Source)
at javax.swing.JTable.tableRowsDeleted(Unknown Source)
at javax.swing.JTable.tableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableChanged(Unknown Source)
at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(Unknown Source)
at javax.swing.table.DefaultTableModel.removeRow(Unknown Source)
at Test$2.actionPerformed(Test.java:39)
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)
The problem is not in the ActionListener, the problem is in the ListSelectionListener; when you delete the row, the row that you're deleting no longer exists; if you want to change the selected row, you have to use the new selected row from the event; probably by using getFirstIndex().
You also need to ensure that the row exists, so that when you delete a row, there's still a valid value to select.
In other words, change your ListSelectionListener to this:
table.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
public void valueChanged(ListSelectionEvent e){
int firstIndex = e.getFirstIndex();
if(firstIndex >= 0 && firstIndex < model.getRowCount()) {
del.setEnabled(true);
numeField.setText(model.getValueAt(firstIndex, 1).toString());
} else {
del.setEnabled(false);
}
}
});

JFileChooser crashes - Java 7

I'm trying to make my program load a txt file with JFileChooser, but it doesn't seem to work. When I press the JButton, the console gives me a lot of errors. Here's the entire code so far:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.JFileChooser;
public class Sudoku extends JFrame{
JPanel mainWindow = new JPanel();
JPanel buttonWindow = new JPanel();
JPanel sudokuArea = new JPanel();
JButton load = new JButton("Load");
JButton solve = new JButton("Solve");
JTextArea sudokuGrid = new JTextArea();
Field field = new Field();
public static void main(String[] args) {
new Sudoku();
}
public Sudoku(){
super("SudokuSolver");
setSize(200,300);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(mainWindow);
mainWindow.setLayout(new BorderLayout());
mainWindow.add(buttonWindow, BorderLayout.SOUTH);
mainWindow.add(sudokuArea, BorderLayout.CENTER);
buttonWindow.add(load);
buttonWindow.add(solve);
sudokuArea.setLayout(new BorderLayout());
sudokuArea.add(sudokuGrid, BorderLayout.CENTER);
sudokuGrid.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
sudokuGrid.setEditable(false);
sudokuGrid.append(field.toString());
load.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
loader();
}
public void loader(){
JFileChooser sumtin = new JFileChooser();
if(sumtin.showOpenDialog() == JFileChooser.APPROVE_OPTION)
{
File filer = sumtin.getSelectedFile();
field.fromFile(filer.getName());
sudokuGrid.setText(field.toString());
mainWindow.revalidate();
mainWindow.repaint();
}
}
} );
setVisible(true);
}
The field method is from another class called Field, but it's not really relevant (I think).
Here's what the console says:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Sudoku$1.loader(Sudoku.java:52)
at Sudoku$1.actionPerformed(Sudoku.java:45)
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 Sour
ce)
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 Sour
ce)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
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 Sour
ce)
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'm not really sure what to make of it, as I don't really know what it means. Any pointers?
EDIT: New errorcode after trying David Colers code:
Sudoku.java:49: error: method showOpenDialog in class JFileChooser cannot be app
lied to given types;
if(sumtin.showOpenDialog() == JFileChooser.APPRO
VE_OPTION)
^
required: Component
found: no arguments
reason: actual and formal argument lists differ in length
1 error
you are not handling the JFileChooser correctly for one thing.
EDIT: changed this keyword to null.
JFileChooser sumtin = new JFileChooser();
if(sumtin.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
File filer = sumtin.getSelectedFile();
field.fromFile(filer.getName());
sudokuGrid.setText(field.toString());
mainWindow.revalidate();
mainWindow.repaint();
}
you missed a few steps:
first create a filechooser
JFileChooser fileChooser = new JFileChooser();
show it, and get the result
int result = fileChooser.showOpenDialog(this);
And if the user has opened a file, you can get it and do what you want
if (result == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
...
}

Speech Recognition jar throwing error on its execution Grammar class not found

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.

NullPointerException while opening new frame

today I'm having a bit of a slight, see, I'm trying to run my code and I'm getting a NullPointerException. The clues in the exception leads me to this function right here:
private void irGuiJuego(JFrame frame){
SwingConsole.run(new GUIJuego(), 800, 600, true);
frame.dispose();
}
Where SwingConsole would have this code:
package utiles;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class SwingConsole {
public static void run(final JFrame frame, final int width, final int height, final boolean exitOnClose) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
if (exitOnClose)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
//frame.setResizable(false);
frame.setVisible(true);
}
});
}
public static void run(final JFrame frame, final int width, final int height, final boolean exitOnClose, final String title) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
if (exitOnClose)
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle(title);
frame.setSize(width, height);
frame.setVisible(true);
}
});
}
}
It is kind of odd, considering that I'm using the same method to open up another frame, in this function to be specific:
private void volverMenuInicio(JFrame frame){
SwingConsole.run(new MenuInicio(), 300, 150, true);
frame.dispose();
}
I'll leave you guys a pastebin of the GUIJuego Frame, since it's sort of excessive to post it here: http://pastebin.com/LSXbc7KE , have the pastebin of the other frame too, in case you need it: http://pastebin.com/hbdd7j84
Edit: Here's the stacktrace, sorry for the lack of it before!
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source) at
java.awt.Container.add(Unknown Source) at
gui.GUIJuego.(GUIJuego.java:113) at
gui.MenuNuevoJuego.irGuiJuego(MenuNuevoJuego.java:95) at
gui.MenuNuevoJuego.access$2(MenuNuevoJuego.java:94) at
gui.MenuNuevoJuego$2.actionPerformed(MenuNuevoJuego.java:74) 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)
Thanks for reading, by the way!
The NPE is being thrown from line 113 in GUIJuego.java, as the stack trace indicates:
panelDatosCiudad.add(arcaLabel);
It's happening because arcaLabel, defined in line 35, is never set to a value, and so you're adding a null JLabel to the container.

Java Swing: Error when dynamically adding JScrollpane to JPanel

Update: If I move the code to be within the mouseclick event (i.e. instead of calling initMarksScreen() I just put the code directly in) it works as expected. So my problem is calling the code in its own method. Does this mean if I want to perform the same steps at another point or on a different button that I have to have the code directly in there instead of in a method I can call?
I am fairly new to Java and trying to create a button that adds a JScrollPane which contains a JTable.
It is called by:
public void mouseClicked(java.awt.event.MouseEvent evt) {
initMarksScreen();
}
The code is:
public final void initMarksScreen() {
String[] columnNames = {"Student ID",
"Last Name",
"Firstname",
"Status",
"Degree",
"Candidate No.",
"Stage",
"Year",
"Code",
"Title",
"Grade Mode",
"Mark",
"Result"};
Object[][] data = {
{"100123456", "Cooper","Sheldon", "Signed Up", "BSc Physics","1201234","1","12","PH1001","Blackholes and Revelations","D",new Integer(99),"P"},
{"100123456", "Cooper","Sheldon", "Signed Up", "BSc Physics","1201234","1","12","PH1025","Astrophysics","D",new Integer(95),"P"}
};
JTable tMarks = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(tMarks);
tMarks.setFillsViewportHeight(true);
panelCentral.add(scrollPane); --!!ERROR AT THIS LINE!!
panelCentral.revalidate();
}
The panel is declared as:
private static final Container panelCentral = null;
As I'm quite new to Java I imagine there is an easy fix but if I need to add an SSCCE.
Everything displays fine until I click the button then the error I get is:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at GUI.mainScreen.initMarksScreen(mainScreen.java:312)
at GUI.mainScreen$2.mouseClicked(mainScreen.java:183)
at java.awt.AWTEventMulticaster.mouseClicked(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)
if panelCentral is JPanel and its not initialized, initialize before add
panelCentral = new JPanel();
panelCentral.add(scrollPane);

Categories

Resources