Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8060295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:56:21+00:00 2026-06-05T09:56:21+00:00

I am getting a NPE in this code. I tried to initilialize each index

  • 0

I am getting a NPE in this code. I tried to initilialize each index but doesn’t seem to work either. Can you please point out what is wrong? Thanks!

    package com.js.teachEnglish;

import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class Piggy_Panel extends JPanel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    int number_lines = getLines(Helper.piggybackResource.getPath());

    protected JLabel title_Label = new JLabel("Piggyback words");

    protected ImageIcon button_image = new ImageIcon(Helper.soundIconResource);

    protected JButton title_Button = new JButton(button_image);

    protected JPanel titlePanel = new JPanel();

    protected JScrollPane pane = new JScrollPane(this);

    protected JLabel[] label_group = new JLabel[number_lines];
    protected JButton[] button_group = new JButton[number_lines];

    protected String[] information = new String[number_lines];

    // Table information
    protected String[] col_titles = new String[] {"",""};
    protected Object[][] table_contents = new Object[number_lines][];

    protected JTable table;

    // constructor
    public Piggy_Panel() {
        setLayout(new GridLayout(number_lines+1,1));

        // scroll pane
        pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

        // getting the information
        readInformation();

        /*
         * adding the title seperately
         * and also adding characteristics for the label
         *  */

        titlePanel = new JPanel();
        titlePanel.setLayout(new FlowLayout());

        // adding the characteristics for the label
        title_Label.setFont(new Font("Arial",Font.PLAIN,36));

        titlePanel.add(title_Label);
        titlePanel.add(title_Button);

        add(titlePanel);

        // calling the ini_labels_buttons() to initlialise the buttons and labels
        ini_labels_buttons();

        // fill the object[][] in
        fill_table();

        // initialising table and adding specific characteristics
        table = new JTable(table_contents, col_titles);
        table.setFillsViewportHeight(true);
        table.setShowHorizontalLines(false);
        table.setShowVerticalLines(false);

        add(table);


        // calling the ini_panel_group() to add everything

        title_Button.addActionListener(new buttonListener());


    }

    // will read the information from the file
    public String[] readInformation() {
        try {
            String line = null;

            BufferedReader br = new BufferedReader(new FileReader(Helper.piggybackResource.getPath()));

            int i = 0;

            while((line = br.readLine()) != null) {
                information[i] = line;
                i++;
            }

            return information;

        }catch(Exception ex) {
            ex.printStackTrace();
        }

        return null;

    }

    // will put out the sound
    // This will output the sound if a button is clicked
    class buttonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            JButton button = (JButton) e.getSource();

            String sayDynamicPath = Helper.sayDynamicResource.getPath() + " ";

            if(button.equals(title_Button)) {
                try {
                    Runtime.getRuntime().exec(sayDynamicPath +
                            title_Label.getText());
                }catch(Exception ex) {
                    JOptionPane.showMessageDialog(null,""+ex,"Unable to produce sound",
                            JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
        }
    }

    // Initialising the JLabels and JButtons and also adding the relevant functions
    // these include icons and information
    public void ini_labels_buttons() {

        // initialising the buttons first
        for (int i = 0; i < button_group.length; i++) {
            button_group[i] = new JButton(button_image);
        }

        // initialising the labels
        for (int i = 0; i < label_group.length; i++) {
            label_group[i] = new JLabel(information[i]);
        }

    }

    public void fill_table() {
        // fill in the first col
        for (int i = 0; i < table_contents.length; i++) {
            table_contents[i][0] = information[i]; // null pointer here
        }

        // fill in the second the col
        for(int j = 0;j < table_contents.length;j++) {
            table_contents[j][1] = button_group[j];
        }
    }

    // this method will get the number of lines and then send it back
    // to the label group
    public int getLines(String path) {
        int n = 0;

        try {
            BufferedReader br = new BufferedReader(new FileReader(path));

            @SuppressWarnings("unused")
            String line = "";

            while((line = br.readLine()) != null) {
                n++;
            }

        }catch(Exception e) {
            e.printStackTrace();
        }

        return n;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:56:22+00:00Added an answer on June 5, 2026 at 9:56 am

    Looing at the NPE’s stacktrace would be useful. 😉

    This creates an array of references to arrays, not arrays of arrays.

    protected Object[][] table_contents = new Object[number_lines][];
    

    This means that

    table_contents[i][0] = information[i]; 
    

    should throw an NPE because table_contents[i] will be null. Looking in a debugger will confirm this.

    The simplest solution is to create an array or arrays with

    protected Object[][] table_contents = new Object[number_lines][2];
    

    BTW I would use one loop instead of two.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep getting this NPE in my application and I can't seem to get
Maybe someone can shed light on this. I'm getting JS's version of an NPE
I'm getting a really strange NPE exception. This is the offending code: private static
Alright, I've been getting a NPE that I can't figure out and it's driving
Getting the above error in following code. How to rectify it. Thanks. Please look
For deleting the app data i am trying to use reflection but getting NPE
Getting this. Site works fine locally, but up on production server I get this
I am getting a NullPointerException raised in this code very rarely. This is part
Getting this error with my fetch's predicate 2011-08-13 13:49:12.405 Codes[16957:10d03] NSlog Array: code BETWEEN
I'm getting an NPE for my EntityManager and can't figure out why - can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.