-1

I have added some menu item in my menu bar in Swing. When I run the program, the menu bar does not show any JMenuItems, and instead throws a NullPointerException.

When I create a menubar in AWT, it works just fine. I have not added any functionality in this program. I use NetBeans, and just dragged and dropped these Swing components into my Panel. please help!

public class test extends javax.swing.JFrame {

/** Creates new form test */
public test() {
    initComponents();
}

/** 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.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

    jPanel1 = new javax.swing.JPanel();
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItem1 = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setName("Form"); // NOI18N

    jPanel1.setName("jPanel1"); // NOI18N

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 279, Short.MAX_VALUE)
    );

    jMenuBar1.setName("jMenuBar1"); // NOI18N

    org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flupertech.FlupertechApp.class).getContext().getResourceMap(test.class);
    jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
    jMenu1.setName("jMenu1"); // NOI18N

    jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
    jMenuItem1.setName("jMenuItem1"); // NOI18N
    jMenu1.add(jMenuItem1);

    jMenuBar1.add(jMenu1);

    jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N
    jMenu2.setName("jMenu2"); // NOI18N
    jMenuBar1.add(jMenu2);

    setJMenuBar(jMenuBar1);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(test.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new test().setVisible(true);
        }
    });
}
// Variables declaration - do not modify
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JPanel jPanel1;
// End of variables declaration
}
JNYRanger
  • 6,829
  • 12
  • 53
  • 81
Ankush Tyagi
  • 63
  • 1
  • 1
  • 6

3 Answers3

1

The errors source is that the ressource loading does not work:

org.jdesktop.application.ResourceMap resourceMap = org.jdesktop
    .application.Application
    .getInstance(flupertech.FlupertechApp.class).getContext()
    .getResourceMap(test.class);
jMenu1.setText(resourceMap.getString("jMenu1.text")); // NOI18N
jMenu1.setName("jMenu1"); // NOI18N

jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
jMenuItem1.setName("jMenuItem1"); // NOI18N
jMenu1.add(jMenuItem1);

jMenuBar1.add(jMenu1);

jMenu2.setText(resourceMap.getString("jMenu2.text")); // NOI18N

If i change it to:

jMenu1.setText("b"); // NOI18N
jMenu1.setName("jMenu1"); // NOI18N

jMenuItem1.setText("c"); // NOI18N
jMenuItem1.setName("jMenuItem1"); // NOI18N
jMenu1.add(jMenuItem1);

jMenuBar1.add(jMenu1);

jMenu2.setText("a"); // NOI18N
jMenu2.setName("jMenu2"); // NOI18N
jMenuBar1.add(jMenu2);

It works well, you may want to find out why your ResourceMap or one of its methods return null.

mambax
  • 1,079
  • 8
  • 17
0

It works now. Just needed to create an object. So i created a new jForm and called it using a jButton. So it passes an object of this form and it works just fine now.

Ankush Tyagi
  • 63
  • 1
  • 1
  • 6
0
from bs4 import BeautifulSoup
import requests
from contextlib import closing
from selenium.webdriver import Chrome # pip install selenium
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import selenium 
import os
import re
import csv

import sys

pPortalBS = requests.get("https://sapeadfs.sapient.com/adfs/ls?wa=wsignin1.0&wtrealm=urn%3asamlcolo%3apeopleportal&wctx=https%3a%2f%2fpeopleportal2.sapient.com%2fmy%2f_layouts%2f15%2fAuthenticate.aspx%3fSource%3d%252F")
pPortal = BeautifulSoup(pPortalBS.content,"html5lib")

#print pPortal

chromedriver = "C:/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
#driver = webdriver.Chrome(chromedriver)
#12,18,30,39,48,76,78
i=0
logged_in_flag = 0
with closing(Chrome(chromedriver)) as driver:


    driver.get("https://www.cdnplanet.com/tools/cdnfinder")

    #button = driver.find_element_by_id('idp')
    page_source = driver.page_source
    #page_source

    # wait for the page to load
    element = WebDriverWait(driver, 10).until(
    EC.visibility_of_element_located((By.ID, "cdn-cdnfinderfull"))
    )
    # store it to string variable
    page_source = driver.page_source

    #logging in

    urls=["http://google.com","https://localhost.xxxxxx","http://twitter.com","http://facebook.com"]
    for url in urls:
        try:
            usernameInput = driver.find_element_by_name("cdn")
            usernameInput.clear()
            usernameInput.send_keys(url)

            button = driver.find_elements_by_xpath(".//*/button[@class='btn btn-large']")

            button[0].click()
            element = WebDriverWait(driver, 10).until(
            EC.visibility_of_element_located((By.ID, "cdnfinderfull-results"))
            )

            with closing(open("Test.csv", "a")) as ofile:   
                fileWriter = csv.writer(ofile)
                page_source = driver.page_source
                pPortal = BeautifulSoup(page_source, "html5lib")
                table = pPortal.find("table")

                print table
        except:
            print ("NO!!!!")
  • Please explain the code that you posted. Why is it working, what was the op doing worng and etc... – Dimitar Spasovski Jul 06 '18 at 10:30
  • DevTools listening on ws://127.0.0.1:51063/devtools/browser/a5199eb1-f069-4d42-8176-34487a454077 [0706/164153.496:ERROR:gl_surface_egl.cc(844)] eglInitialize D3D9 failed with error EGL_NOT_INITIALIZED [0706/164153.498:ERROR:gl_initializer_win.cc(232)] GLSurfaceEGL::InitializeOneOff failed. [0706/164153.521:ERROR:viz_main_impl.cc(199)] Exiting GPU process due to errors during initialization [8364:11748:0706/164154.446:ERROR:gpu_process_transport_factory.cc(1017)] Lost UI shared context. ERROR:command_buffer_proxy_impl.cc(114)] ContextResult::kFatalFailure: Shared memory handle is not valid – Ankush Tyagi Jul 06 '18 at 11:12