我正在关注Netbeans CRUD应用程序教程(修改为使用我自己的数据库),我收到的这个错误根本无法解决。
该错误表示"非法启动类型';'预计,找不到符号
这是我的代码:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.photo.viewer;
import demo.Photos;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;
/**
* Top component which displays something.
*/
@ConvertAsProperties(
dtd = "-//com.photo.viewer//photoViewer//EN",
autostore = false
)
@TopComponent.Description(
preferredID = "photoViewerTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE",
persistenceType = TopComponent.PERSISTENCE_ALWAYS
)
@TopComponent.Registration(mode = "explorer", openAtStartup = true)
@ActionID(category = "Window", id = "com.photo.viewer.photoViewerTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(
displayName = "#CTL_photoViewerAction",
preferredID = "photoViewerTopComponent"
)
@Messages({
"CTL_photoViewerAction=photoViewer",
"CTL_photoViewerTopComponent=photoViewer Window",
"HINT_photoViewerTopComponent=This is a photoViewer window"
})
public final class photoViewerTopComponent extends TopComponent {
public photoViewerTopComponent() {
initComponents();
setName(Bundle.CTL_photoViewerTopComponent());
setToolTipText(Bundle.HINT_photoViewerTopComponent());
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(27, 27, 27)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(129, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(16, 16, 16)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(200, Short.MAX_VALUE))
);
}// </editor-fold>
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea jTextArea1;
// End of variables declaration
@Override
public void componentOpened() {
// TODO add custom code on component opening
}
@Override
public void componentClosed() {
// TODO add custom code on component closing
}
void writeProperties(java.util.Properties p) {
// better to version settings since initial version as advocated at
// http://wiki.apidesign.org/wiki/PropertyFiles
p.setProperty("version", "1.0");
// TODO store your settings
}
void readProperties(java.util.Properties p) {
String version = p.getProperty("version");
// TODO read your settings according to their version
}
EntityManager entityManager = Persistence.createEntityManagerFactory("catalogueLibraryPU").createEntityManager();
Query query = entityManager.createNamedQuery("Photos.findAll");
List<Photos> resultList = query.getResultList();
for (Photos c : resultList) {
jTextArea1.append(c.getTitle() + " (" + c.getLocation() + ")" + "\n");
}
}
错误发生在第4行:
for (Photos c : resultList) {
jTextArea1.append(c.getTitle() + " (" + c.getLocation() + ")" + "\n");
}
我知道这很简单,但我无法弄清楚是什么或如何解决它。我确信这是一个错位的&#39 ;;&#39;还是什么?
毫无疑问,我知道我是Java新手。
我完成了教程所说的所有内容,但似乎无法解决这个问题......
任何帮助都会很棒!
由于
答案 0 :(得分:3)
我认为你错过了方法块。可能你想在某些方法中包含下面的代码块。
EntityManager entityManager = Persistence.createEntityManagerFactory("catalogueLibraryPU").createEntityManager();
Query query = entityManager.createNamedQuery("Photos.findAll");
List<Photos> resultList = query.getResultList();
for (Photos c : resultList) {
jTextArea1.append(c.getTitle() + " (" + c.getLocation() + ")" + "\n");
}