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 9168081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:32:48+00:00 2026-06-17T15:32:48+00:00

Apparently Java thinks my constructor code is not important, so it completely ignores it

  • 0

Apparently Java thinks my constructor code is not important, so it completely ignores it and then yells at me with a NullPointerException when I try to access an ArrayList that I thought was initialized. Only when I add an arbitrary parameter to my constructor does Java think it’s worth looking at.

import java.util.ArrayList;

public class DataManager {
    ArrayList<Variable> vars;

    public DataManager() {
        vars = new ArrayList<Variable>();
    }

    public void createVar(String type, String name, String strValue, int numValue) {
        vars.add(new Variable(type, name, strValue, numValue));
    }
}

And the code that calls this:

DataManager data = new DataManager();
data.createVar(...);

Variable class:

class Variable {
    String type;
    String name;
    String strValue;
    int numValue;

    public Variable(String type, String name, String strValue, int numValue) {
            this.type = type; this.name = name;
            this.strValue = strValue;
            this.numValue = numValue;
    }
}

Running this results in

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 1
at Parser.start(Parser.java:25)
at SudoCode.go(SudoCode.java:10)
at SudoCode.main(SudoCode.java:6)

So… what’s the deal? Why are constructors ignored when they aren’t parameterized? It’s just not very intuitive. Was this some sort of design choice that I can’t see the obvious implications of? If so, enlighten me. And should I just add an arbitrary parameter so the constructor is executed, or should I create and explicitly call a method designed solely to initialize my ArrayList?

Thanks!

  • 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-17T15:32:49+00:00Added an answer on June 17, 2026 at 3:32 pm

    Your assumption is false. You cannot instantiate an object without having its constructor execute.

    If you define a class without a constructor, Java will create an implicit (“default”) constructor without parameters.

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

Sidebar

Related Questions

I need to add a user profile variable in java, apparently %userprofile% does not
java -Xms is apparently not having an affect on the amount of memory the
Apparently Java only plays .au audio files and I want to play mp3 files.
In Java, apparently, String s = foo is preferred over String s = new
Apparently a colon is used in multiple ways in Java. Would anyone mind explaining
Apparently mutableCopy copies by reference, not value. Ie if I do this: NSMutableArray arrayA
Apparently, there's been a big brouhaha over whether or not Python needs tail-call optimization
I was recently asked a question, apparently in an Interview, about extending the java.lang.RuntimeException.
I'm not experienced with java applications but I found out that finding static pointers
Apparently unapply/unapplySeq in extractor objects do not support implicit parameters. Assuming here an interesting

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.