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

  • SEARCH
  • Home
  • 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 6893977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:43:31+00:00 2026-05-27T06:43:31+00:00

I have the following code which uses one of the JTextAreas from an array

  • 0

I have the following code which uses one of the JTextAreas from an array of such Objects to assign the Insets of a JTextArea for my program. The code is as follows:

import javax.swing.*;
import java.awt.*; 

public class MainComponents
{ 
     private final static JTextField[] Input = new JTextField[10];

     public MainComponents(final Container P, final JFrame frame)
     { 

         P.setLayout(null);
         final Insets insets = P.getInsets();
         final Dimension InputInsets = Input[0].getPreferredSize();  //?

         ...
      }
}

Where I have the //? is where I get an Exception that tells me java.lang.NullPointerException occurs at this line. I cannot find the reason that Input[0] would be null if I assigned them alll to be JTextAreas. Am I declaring something incorrectly in the array?

Thank you.

  • 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-05-27T06:43:32+00:00Added an answer on May 27, 2026 at 6:43 am

    You have initialized an array of JTextField. But each element in the array is still not initialized.

    private final static JTextField[] Input = new JTextField[10];
    
    Input[0] = new JTextField(); //And then perform operation on 0th element
    

    To throw more light on this,

    Whenever you initialize any primitive array in java, it will allocate memory continuously based on the passed data type.

    For example,

    int[] intArray = new int[10]; // Assuming int is 4 bytes, 40 bytes allocated continuously
    double[] doubleArray = new double[10]; // Assuming double is 8 bytes, 80 bytes allocated continuously.
    

    And note that intArray or doubleArray is a reference that is going to point to the starting memory location of the allocated value. This is the reason arrays are faster while searching by index. when you do a intArray[5], all it does is go to initial address of intArray + (5 * sizeof(integer)) will give the value directly.

    However, things are a bit differnt in case of Objects. When I do

    Object[] objArray = new Object[10];
    

    This will create again a continuous 10 references in memory. Assuming a reference is 2 bytes, it will need 20 bytes continously. And all these are just references that are pointing to no where, ie they are null. It is our duty to invoke them explicitly.

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

Sidebar

Related Questions

I have the following code which re-uses a CookieContainer which logs in on the
I have the following code which uses the repository pattern, backed by a fluent
I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
I have the following code which should put programs startable in Bash. if [
I have the following code which reads in the follow file, append a \r\n
I have the following code which I stripped out of any non-essential lines to
I have the following code which ends up forever reading '/proc/cpuinfo' as it keeps

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.