Netbeans "illegal start of type"错误



我正在学习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");
    } 

我知道这很简单,但我不知道该怎么解决。我确信这是一个放错地方的";"还是什么?

毫无疑问,你知道我是Java的新手。

我已经完成了教程中所说的一切,但似乎无法理解。。。

任何帮助都将是伟大的!

感谢

我想您错过了方法块。。也许你想用某种方法包装下面的代码块。

   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");
  } 

相关内容

  • 没有找到相关文章