Home > Software engineering >  URL to image always return null
URL to image always return null

Time:07-07

Why does the file name not point to the picture? I tried to use a slash at the beginning but it nevertheless did not work. How should the url look like if the image is in the same folder? I don't know why this does not work. the Login_Form and the download.png file are both in the same folder.

package com.teamsea.langapp;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;

import static com.sun.javafx.scene.control.skin.Utils.getResource;

public class Login_Form extends javax.swing.JFrame {

    /**
     * Creates new form Login_Form
     */


    private void jTextField_Username_VarActionPerformed() {    }

    private void jTextField_Username_VarMouseClicked() {    }

    private void jPasswordField_Password_VarActionPerformed() {    }

    private void jButton_Login_VarKeyPressed() {    }

    public Login_Form() {
        initComponents();
        jButton_Login_Var.setBackground(Color.decode("#f8c200"));
        jButton_Login_Var.setOpaque(true);
    }

    /**
     * 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.
     */




    private void jLabel6_VarMouseClicked() {
        new Register_Form().setVisible(true);
        Login_Form.this.setVisible(false);
    }



    private void jButton_Login_VarActionPerformed() {
        String username_Var =  jTextField_Username_Var.getText();
        String password_Var =  String.valueOf(jPasswordField_Password_Var.getPassword());
        String sha256hex_Var = org.apache.commons.codec.digest.DigestUtils.sha256Hex(password_Var);
        String registeredPassword_Var = UseTable.get("User", "username", username_Var, 4);
        if(sha256hex_Var.equals(registeredPassword_Var)){
            System.out.println("Login "  UseTable.get("User", "username", username_Var, 1));
            Application.sessionUser = Integer.parseInt(Objects.requireNonNull(UseTable.get("User", "username", username_Var, 1)));
            new MainMenu().setVisible(true);
            Login_Form.this.setVisible(false);
        } else {
            error_Label_Var.setText("Wrong credentials!");
        }
    }



    public static void main(String[] args) {

        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 | InstantiationException | IllegalAccessException |
                 UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login_Form.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(() -> new Login_Form().setVisible(true));
    }

    private javax.swing.JLabel error_Label_Var;
    private javax.swing.JButton jButton_Login_Var;
    private javax.swing.JPasswordField jPasswordField_Password_Var;
    private javax.swing.JTextField jTextField_Username_Var;

    private void initComponents() {

        JPanel jPanel1_var = new JPanel();
        JPanel jPanel2_Var = new JPanel();
        JPanel jPanel3_var = new JPanel();
        JLabel jLabel4_Var = new JLabel();
        jTextField_Username_Var = new javax.swing.JTextField();
        JLabel jLabel5_Var = new JLabel();
        jButton_Login_Var = new javax.swing.JButton();
        JLabel jLabel6_Var = new JLabel();
        jPasswordField_Password_Var = new javax.swing.JPasswordField();
        error_Label_Var = new javax.swing.JLabel();
        JLabel jLabel3_Var = new JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setSize(new java.awt.Dimension(414, 896));

        jPanel1_var.setBackground(new java.awt.Color(0, 0, 0));

        jPanel2_Var.setBackground(new java.awt.Color(255, 255, 255));

        jPanel3_var.setBackground(new java.awt.Color(51, 102, 255));
        jPanel3_var.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel4_Var.setFont(new java.awt.Font("Tahoma", Font.PLAIN, 14)); // NOI18N
        jLabel4_Var.setForeground(new java.awt.Color(255, 255, 0));
        jLabel4_Var.setText("Password");
        jPanel3_var.add(jLabel4_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 140, -1, -1));

        jTextField_Username_Var.setForeground(new java.awt.Color(255, 255, 0));
        jTextField_Username_Var.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField_Username_Var.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTextField_Username_VarMouseClicked();
            }
        });
        jTextField_Username_Var.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField_Username_VarActionPerformed();
            }
        });
        jPanel3_var.add(jTextField_Username_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 60, 264, 49));

        jLabel5_Var.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jLabel5_Var.setForeground(new java.awt.Color(255, 255, 0));
        jLabel5_Var.setText("Username");
        jPanel3_var.add(jLabel5_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, -1, -1));

        jButton_Login_Var.setFont(new java.awt.Font("Trebuchet MS", Font.BOLD, 18)); // NOI18N
        jButton_Login_Var.setText("Login");
        jButton_Login_Var.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        jButton_Login_Var.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_Login_VarActionPerformed();
            }
        });
        jButton_Login_Var.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButton_Login_VarKeyPressed();
            }
        });
        jPanel3_var.add(jButton_Login_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 240, 151, 40));

        jLabel6_Var.setForeground(new java.awt.Color(255, 255, 0));
        jLabel6_Var.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6_Var.setText("or click here to register..");
        jLabel6_Var.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6_VarMouseClicked();
            }
        });
        jPanel3_var.add(jLabel6_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 300, 360, -1));

        jPasswordField_Password_Var.setForeground(new java.awt.Color(255, 255, 0));
        jPasswordField_Password_Var.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jPasswordField_Password_Var.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField_Password_VarActionPerformed();
            }
        });
        jPanel3_var.add(jPasswordField_Password_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 160, 264, 49));

        error_Label_Var.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        error_Label_Var.setMaximumSize(new java.awt.Dimension(0, 11));
        error_Label_Var.setPreferredSize(new java.awt.Dimension(0, 11));
        jPanel3_var.add(error_Label_Var, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 220, 394, -1));

        jLabel3_Var.setBackground(new java.awt.Color(51, 153, 255));
        jLabel3_Var.setFont(new java.awt.Font("Tw Cen MT", 1, 48)); // NOI18N
        jLabel3_Var.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3_Var.setIcon(new javax.swing.ImageIcon(getResource("/download.png"))); // NOI18N
        jLabel3_Var.setText("LangApp");

        javax.swing.GroupLayout jPanel2_VarLayout = new javax.swing.GroupLayout(jPanel2_Var);
        jPanel2_Var.setLayout(jPanel2_VarLayout);
        jPanel2_VarLayout.setHorizontalGroup(
                jPanel2_VarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel3_var, javax.swing.GroupLayout.DEFAULT_SIZE, 422, Short.MAX_VALUE)
                        .addComponent(jLabel3_Var, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel2_VarLayout.setVerticalGroup(
                jPanel2_VarLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2_VarLayout.createSequentialGroup()
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel3_Var, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(48, 48, 48)
                                .addComponent(jPanel3_var, javax.swing.GroupLayout.PREFERRED_SIZE, 344, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1_varLayout = new javax.swing.GroupLayout(jPanel1_var);
        jPanel1_var.setLayout(jPanel1_varLayout);
        jPanel1_varLayout.setHorizontalGroup(
                jPanel1_varLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel2_Var, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1_varLayout.setVerticalGroup(
                jPanel1_varLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1_varLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jPanel2_Var, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

        pack();
    }
}

enter image description here

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException: Cannot invoke "java.net.URL.toExternalForm()" because "location" is null
    at java.desktop/javax.swing.ImageIcon.<init>(ImageIcon.java:232)
    at com.teamsea.langapp.Login_Form.initComponents(Login_Form.java:129)
    at com.teamsea.langapp.Login_Form.<init>(Login_Form.java:26)
    at com.teamsea.langapp.Login_Form$7.run(Login_Form.java:242)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:318)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:773)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:720)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:714)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

Process finished with exit code 0

CodePudding user response:

instead of the complete path can you try getClass().getResource("download.png")

This is because you are already in the path of com.teamsea.langapp

  •  Tags:  
  • java
  • Related