This method is supposed to replace the features in the database with new features I pass to it.
private static void replaceBoundaryShape(FeatureCollection<SimpleFeatureType, SimpleFeature> dbFeatures) throws IOException, CQLException{
FeatureStore<SimpleFeatureType, SimpleFeature> fs = null;
FeatureSchemaFactory factory = FeatureSchemaFactory.init();
DataStore geoStore = initDataStore();
try{
fs = (FeatureStore) geoStore.getFeatureSource(factory.getFieldFeatureSchema().getTypeName());
fs.setTransaction(new DefaultTransaction());
for(FeatureIterator<SimpleFeature> iterator = dbFeatures.features(); iterator.hasNext();){
SimpleFeature simpleFeature = iterator.next();
Number fieldId = (Number) simpleFeature.getAttribute(FeatureSchemaFactory.FIELD_ID);
Filter filter = createSearchByIdFilter(fieldId.longValue());
fs.removeFeatures(filter);
}
fs.addFeatures(dbFeatures);
fs.getTransaction().commit();
}catch(CQLException ex){
throw new CQLException(ex.getMessage());
}
}
this is where I create the feature collection passed to replace boundary shape
private static FeatureCollection<SimpleFeatureType, SimpleFeature> createBoundaryFeatures(SimpleFeatureType dbSchema, Collection<FieldFeature> fields){
List<SimpleFeature> result = new ArrayList<SimpleFeature>(fields.size());
SimpleFeatureBuilder builder = new SimpleFeatureBuilder(dbSchema);
for(FieldFeature f: fields){
builder.set("BNDRS", f.getGeometries());
builder.set("FIELD", f.getField());
builder.set("FID", f.getFid());
builder.set("GROWER", f.getGrower());
builder.set("SIMPLIFIED_BNDRS", f.getSimplifiedGeometries());
result.add(builder.buildFeature(null));
}
return DataUtilities.collection(result);
}
This is where I get my field features from a file path and a specified field:
private static Collection<FieldFeature> convertShpFile(String filePath, Field field) throws MalformedURLException, IOException{
#SuppressWarnings("deprecation")
ShapefileDataStore source = new ShapefileDataStore(new File(filePath).toURL());
Collection<FieldFeature> result = new LinkedList<FieldFeature>();
DefaultQuery query = new DefaultQuery();
query.setCoordinateSystemReproject(DefaultGeographicCRS.WGS84);
FeatureCollection queryResult = source.getFeatureSource().getFeatures(query);
FeatureIterator<SimpleFeature> fi = queryResult.features();
while(fi.hasNext()){
SimpleFeature feature = fi.next();
FieldFeature fieldFeature = new FieldFeature();
Object defaultGeometry = feature.getDefaultGeometry();
if(defaultGeometry instanceof Polygon){
Polygon polygon = (Polygon) defaultGeometry;
Polygon[] polygons = {polygon};
defaultGeometry = geometryFactory.createMultiPolygon(polygons);
}
fieldFeature.setField(field.getId());
fieldFeature.setGeometries(defaultGeometry);
fieldFeature.setSimplifiedGeometries(GeoHelper.simplifyGeometry((Geometry) defaultGeometry));
result.add(fieldFeature);
}
fi.close();
source.dispose();
return result;
}
The error occurs on the fs.addFeatures(dbFeatures) line of the replaceBoundaryShape method.
Removing features does not throw an error, but when I call the addFeatures function I receive this AbstractMethodError:
java.lang.AbstractMethodError: org.geotools.jdbc.PreparedStatementSQLDialect.setGeometryValue(Lcom/vividsolutions/jts/geom/Geometry;IILjava/lang/Class;Ljava/sql/PreparedStatement;I)V
The error appears to be thrown when the features are being inserted into the JDBCDataStore
EDIT: Here is the full stack trace
java.lang.AbstractMethodError: org.geotools.jdbc.PreparedStatementSQLDialect.setGeometryValue(Lcom/vividsolutions/jts/geom/Geometry;IILjava/lang/Class;Ljava/sql/PreparedStatement;I)V
at org.geotools.jdbc.JDBCDataStore.insertSQLPS(JDBCDataStore.java:4005)
at org.geotools.jdbc.JDBCDataStore.insert(JDBCDataStore.java:1582)
at org.geotools.jdbc.JDBCDataStore.insert(JDBCDataStore.java:1545)
at org.geotools.jdbc.JDBCInsertFeatureWriter.write(JDBCInsertFeatureWriter.java:76)
at org.geotools.data.InProcessLockingManager$1.write(InProcessLockingManager.java:337)
at org.geotools.data.store.ContentFeatureStore.addFeature(ContentFeatureStore.java:309)
at org.geotools.data.store.ContentFeatureStore.addFeatures(ContentFeatureStore.java:263)
at com.conserviscorp.shape.upload.FieldDAO.replaceBoundaryShape(FieldDAO.java:146)
at com.conserviscorp.shape.upload.FieldDAO.updateFieldData(FieldDAO.java:116)
at com.conserviscorp.ui.MatchedFieldFrame$2.actionPerformed(MatchedFieldFrame.java:83)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
The issue was that I was using the wrong version of geotools. When adding items to the dependencies I was using the 10-beta version in some parts, and the current version in other parts.
Related
This is my populate account method which takes the values from my database columns and stores them within an arraylist of objects of the account class that i made which has fields like username and password.
The second part of the code will show if you scroll down
private ArrayList<account> populateAccount() throws SQLException{
Connection c = DatabaseUtilityClass.createNewConnection();
ArrayList<account> list = new ArrayList();
try{
String q = "Select * from LOGIN";
Statement stmt = (Statement)c.createStatement();
try(ResultSet rs= stmt.executeQuery(q)){
while(rs.next()){
account a = new account();
a.setUsername(rs.getString("USERNAME"));
a.setPassword(rs.getString("PASSWORD"));
list.add(a);
}
}
}
catch(Exception e){
e.printStackTrace();
System.out.println("err");
}
c.close();
return list;
}
This is the second part of my code which should store the arraylist of objects filled with the database info and then take user text and use a for each loop to iterate through each object checking if the username and password values are the same as the one the user entered via a jTextfield and password field. The errors are on the bottom.
private void LoginActionPerformed(java.awt.event.ActionEvent evt) {
try {
accountCheck= populateAccount();
} catch (SQLException ex) {
Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
}
String username = user.getText();
String password = pass.getText();
for (account a:accountCheck) {
if(a.getUsername().equals(username) && a.getPassword().equals(password)){
JPanel p = (JPanel)this.getParent();
CardLayout c = (CardLayout)p.getLayout();
c.show(p, PanelConstants.Home);
}
}
}
java.lang.UnsupportedOperationException: Not supported yet.
at boomiautomationapp.account.<init>(account.java:21)
at boomiautomationapp.LoginPage.populateAccount(LoginPage.java:50)
at boomiautomationapp.LoginPage.LoginActionPerformed(LoginPage.java:146)
at boomiautomationapp.LoginPage.access$000(LoginPage.java:28)
at boomiautomationapp.LoginPage$1.actionPerformed(LoginPage.java:94)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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)
at java.awt.Component.processMouseEvent(Component.java:6516)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6281)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4872)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4698)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:747)
at java.awt.EventQueue.access$300(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:706)
at java.awt.EventQueue$3.run(EventQueue.java:704)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:720)
at java.awt.EventQueue$4.run(EventQueue.java:718)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:717)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I am trying to run the following code in which i have to save my file as .txt...but whenever I click on 'Save' button..it throws an exception..
Also i want to know how could I save the contents of my textarea line by line in the txt file (including \n).
Here is the code
try
{
int val = jfc.showSaveDialog(jf);
int x =0;
String line;
if(val == JFileChooser.APPROVE_OPTION)
{
File fs=jfc.getSelectedFile();
if(!fs.exists())
{
fs.createNewFile();
FileWriter fw=new FileWriter(fs.getAbsolutePath()+".txt");
BufferedWriter bf=new BufferedWriter(fw);
BufferedReader br = new BufferedReader(new StringReader(ja.getText()));
while((line= br.readLine())!=null)
{
x++;
if(line.charAt(x)=='\n')
fw.write('\n');
else
fw.write(line+"\n");
}
fw.close();
}
else
{
}
}
}
catch(Exception e2)
{
e2.printStackTrace();
JOptionPane.showMessageDialog(null,"Cannot save file");
}
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(String.java:658)
at notepad$1.actionPerformed(notepad.java:100)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Change your current code from
while((line= br.readLine())!=null)
{
x++;
if(line.charAt(x)=='\n')
fw.write('\n');
else
fw.write(line+"\n");
}
to
while((line= br.readLine())!=null) {
fw.write(line+"\n");
}
You are checking the first char in the first line, the second char in the second line so on.
Try changing
if(line.charAt(x)=='\n')
to
if(line.endsWith('\\n'))
Also you should escape line ends. Use \\n instead of \n.
I am using Netbeans IDE to develop a desktop Java App. I have an embedded Derby database which has a few tables. All of the tables are in the APP schema. The problem I am having is that a query I am making to the DB is coming back with the above exception. I know the table exists and if I run the exact same SQL query from inside the editor it functions as expected. I am looking for help with the problem but more so looking for help as to how you would troubleshoot a problem like this. I have attached some code and the stack trace. Please don't hesitate to ask for more info if it will help
public static void doKeywordListDisplayLogic() {
try {
SimpleQuery keywordIdQuery = new SimpleQuery();
keywordIdQuery.setSelectExpressionList("*");
keywordIdQuery.setDataSource("KEYWORD_LOOKUP");
keywordIdQuery.setWherePredicates("SDS_NUMBER");
keywordIdQuery.setComparisonOperator("=");
keywordIdQuery.setQueryPredicate(selectedSdsNumber);//selectedSdsNumber comes from a previous query and table row selection.
keywordIdQuery.simpleQuery();
String keyId = rs.getString("KEY_ID");
System.out.println(keyId);
} catch (Exception e) {
e.printStackTrace();
}
}
public void simpleQuery() {
try {
conn = DbCommunication.JavaConnect.ConnectDb();
String dbQuery = "SELECT ".concat(selectExpressionList).concat(" FROM ").concat(dataSource).concat(" WHERE ").concat(wherePredicates).concat(" ").concat(comparisonOperator).concat(" ").concat(queryPredicate);
System.out.println(dbQuery);
PreparedStatement pst = conn.prepareStatement(dbQuery);
rs = pst.executeQuery();
} catch (Exception e) {
e.printStackTrace(System.out);
JOptionPane.showMessageDialog(null, e);
}
}
***THE SELECT SQL QUERY BELOW IS THE FINAL STRING SUBMITTED TO THE DB***
SELECT * FROM LOCATION_LOOKUP WHERE SDS_NUMBER = 998
java.sql.SQLException: There is no column named: KEY_ID.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ResultSet.getString(Unknown Source)
at RecordHandling.PopulateRecord.doKeywordListDisplayLogic(PopulateRecord.java:264)
at RecordHandling.PopulateRecord.doRowRetrieval(PopulateRecord.java:202)
at Gui.mainFrame.jButton7ActionPerformed(mainFrame.java:717)
at Gui.mainFrame.access$400(mainFrame.java:25)
at Gui.mainFrame$5.actionPerformed(mainFrame.java:232)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
Caused by: org.apache.derby.client.am.SqlException: There is no column named: KEY_ID.
at org.apache.derby.client.am.ColumnMetaData.findColumnX(Unknown Source)
at org.apache.derby.client.am.ResultSet.findColumnX(Unknown Source)
... 42 more
There is no column named: KEY_ID
Instead of retrieving using column name,
String keyId = rs.getString("KEY_ID");
try retrieve using index:
String keyId = rs.getString(indexOfKeyId);//if key_id is at column 2 in table, then rs.getString(2)
To troubleshoot, display all value from rs, and see if your desired output(key id) is there.
I'm trying to run my simple java-program, but have a big problem.
It's connected with GUI. It can not show any created window. I attach stack-trace and one class.
#ConvertAsProperties(
dtd = "-//org.shop.viewer//ShopViewer//EN",
autostore = false
)
#TopComponent.Description(
preferredID = "ShopViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
#TopComponent.Registration(mode = "explorer", openAtStartup = false)
#ActionID(category = "Window", id = "org.shop.viewer.ShopViewerTopComponent")
#ActionReference(path = "Menu/Window" /*, position = 333 */)
#TopComponent.OpenActionRegistration(
displayName = "#CTL_ShopViewerAction",
preferredID = "ShopViewerTopComponent"
)
#Messages({
"CTL_ShopViewerAction=ShopViewer",
"CTL_ShopViewerTopComponent=ShopViewer Window",
"HINT_ShopViewerTopComponent=This is a ShopViewer window"
})
public final class ShopViewerTopComponent extends TopComponent {
public ShopViewerTopComponent() {
initComponents();
setName(Bundle.CTL_ShopViewerTopComponent());
setToolTipText(Bundle.HINT_ShopViewerTopComponent());
EntityManager entityManager = Persistence.createEntityManagerFactory("ShopLibraryPU").createEntityManager();
Query query = entityManager.createNamedQuery("Shops.findAll");
List<Shops> resultList = query.getResultList();
for (Shops c : resultList) {
jTextArea1.append(c.getTitle() +"dqwdqwdqwdqwd"+ "\n");
}
}
java.lang.IllegalStateException: Cannot find TopComponent with preferredID ShopViewerTopComponent, see IDE log for more details.
at org.openide.windows.OpenComponentAction.actionPerformed(OpenComponentAction.java:91)
at org.openide.awt.AlwaysEnabledAction$1.run(AlwaysEnabledAction.java:198)
at org.openide.util.actions.ActionInvoker$1.run(ActionInvoker.java:95)
at org.openide.util.actions.ActionInvoker.doPerformAction(ActionInvoker.java:116)
at org.openide.util.actions.ActionInvoker.invokeAction(ActionInvoker.java:99)
at org.openide.awt.AlwaysEnabledAction.actionPerformed(AlwaysEnabledAction.java:201)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:159)
[catch] at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
I think that a misstake is in TopComponent-constructor.
Thanks a lot.
This problem "cannot find TopCoponent with preferredID..." happens frequently when a problem occurs before loading the TopComponent.
A simple example : you try to load something in the constructor that fails (at any depth).
This unexplicit message made me mad more than once !
Hope this helps.
When I run the code, I get an error. The strange thing is when I change files.getTimeScore(); to files.getLineScore();, it executes without errors. However, these functions are almost identical to each other.
When I run the getTimeScore() method from a main in the FileIO class, then it works fine.
Errors
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at tetris.FileIO.loadHighscores(FileIO.java:52)
at tetris.FileIO.getTimeScores(FileIO.java:31)
at tetris.HighScores.<init>(HighScores.java:39)
at tetris.Menu$2.actionPerformed(Menu.java:74)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
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)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2719)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:682)
at java.awt.EventQueue$3.run(EventQueue.java:680)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:696)
at java.awt.EventQueue$4.run(EventQueue.java:694)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)
Code
Highscores class
public HighScores(){
// init A
super("Highscores");
c=getContentPane();
test = new String[100][2];
files = new FileIO();
mainPanel = new JPanel();
test = files.getTimeScores();
timeTable = new JTable(test,timeTitles );
test = files.getLineScores();
lineTable = new JTable(test,lineTitles );
FileIO class
import java.io.File;
import java.io.InputStream;
import java.io.Writer;
import java.util.Scanner;
public class FileIO {
private File file;
private Scanner filescScanner, lineScanner;
private Writer fileWriter, lineWriter;
private String[][] data;
public FileIO () {
data = new String[100][2];
}
public String[][] getLineScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("LineHighscores.txt"));
}
public String[][] getTimeScores(){
return this.loadHighscores(this.getClass().getResourceAsStream("TimeHighscores.txt"));
}
public String[][] loadHighscores( InputStream resourceStream){
int x=0;
String test = "";
filescScanner = new Scanner(resourceStream);
while(filescScanner.hasNextLine()&& x<100) {
lineScanner = new Scanner(filescScanner.nextLine());
lineScanner.useDelimiter("-/-");
data[x][0]=lineScanner.next();//name
data[x][1]=lineScanner.next();//data
x++;
}
lineScanner.close();
filescScanner.close();
return data;
}
You don't test those files can be accessed. i.e. if resourceStream is null.
Using this approach is not idea as you cannot easily update these files if you want add a high score.
line scanner is null for TimeHighscores.txt file. So it means that the while loop was not executed - not even once.
I guess your lineScanner is null. Can't see where you intialize it in FileIO.
You only intialize it in the while-clause.
So if your File is empty, it is still null when you are trying to close it.