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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:50:09+00:00 2026-06-17T02:50:09+00:00

I’m getting a NullPointerException error in Eclipse. Code as it stands right now: Java:

  • 0

I’m getting a NullPointerException error in Eclipse. Code as it stands right now:

Java:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System. * ;

public class MadLib {
    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();

    public MadLib() {}

    public MadLib(String fileName) {
        //load stuff
        try {
            Scanner file = new Scanner(new File(fileName));
        }
        catch(Exception e) {
            out.println("Houston we have a problem!");
        }
    }

    public void loadNouns() {
        nouns = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("nouns.dat");
            while (chopper.hasNext()) {
                nouns.add(chopper.next());
            }
            chopper.close();
            out.println(nouns);
        }
        catch(Exception e) {
            out.println("Will");
        }
    }

    public void loadVerbs() {
        verbs = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("verbs.dat");
            while (chopper.hasNext()) {
                verbs.add(chopper.next());
            }
            chopper.close();
        }
        catch(Exception e) {
            out.println("run");
        }
    }

    public void loadAdjectives() {
        adjectives = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner("adjectives.dat");
            while (chopper.hasNext()) {
                adjectives.add(chopper.next());
            }
            chopper.close();
        }
        catch(Exception e) {}
    }

    public String getRandomVerb() {
        String verb = "";
        int num = 0;
        num = (int)(Math.random() * verbs.size());
        verb = verbs.get(num);
        return verb;
    }

    public String getRandomNoun() {
        String noun = "";
        int num = 0;
        num = (int)(Math.random() * nouns.size());
        noun = nouns.get(num);
        return noun;
    }

    public String getRandomAdjective() {
        String adj = "";
        int num = 0;
        num = (int)(Math.random() * adjectives.size());
        adj = adjectives.get(num);
        return adj;
    }

    public String toString() {
        String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
        return output;
    }
}

Eclipse is pointing to the issue occurring at the linenum = (int)(Math.random()*nouns.size()); but this seems to not make much sense to me.

I have the private ArrayList<String> initialized at the method loadNouns. I origianlly had ArrayList<String> nouns initialized at getRandomNoun(), but that threw a different error, so I was advised to move the initialization statement to the loadNouns method.

Runner Class:

import static java.lang.System.*;

public class Lab16d 
public static void main( String args[] ) {
     //make a new MadLib
     MadLib fun = new MadLib();
     out.println(fun);
}

Update:

The real issue appears to be that ArrayList<String> nouns never is “loaded up” with the separate strings which are supposed to be scanned in from the nouns.dat file

Update 2:

Java:

import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System. * ;

public class MadLib {
    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();

    public MadLib() {
        loadNouns();
        loadVerbs();
        loadAdjectives();
        out.println(nouns);
    }

    public MadLib(String fileName) {
        //load stuff
        loadNouns();
        loadVerbs();
        loadAdjectives();
        try {
            Scanner file = new Scanner(new File(fileName));
        }
        catch(Exception e) {
            out.println("Houston we have a problem!");
        }
    }

    public void loadNouns() {
        nouns = new ArrayList < String > ();
        try {
            //nouns = new ArrayList<String>();
            String nou = "";
            Scanner chopper = new Scanner(new File("nouns.dat"));

            //chopper.nextLine();
            while (chopper.hasNext()) {
                nou = chopper.next();
                out.println(nou);
                nouns.add(nou);
                //chopper.nextLine();
            }
            //chopper.close();
            out.println(nouns.size());
        }
        catch(Exception e) {
            out.println("Will");
        }
    }

    public void loadVerbs() {
        verbs = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner(new File("verbs.dat"));
            while (chopper.hasNext()) {
                verbs.add(chopper.next());
                chopper.nextLine();
            }
            chopper.close();
        }
        catch(Exception e) {
            out.println("run");
        }
    }

    public void loadAdjectives() {
        adjectives = new ArrayList < String > ();
        try {
            Scanner chopper = new Scanner(new File("adjectives.dat"));
            while (chopper.hasNext()) {
                adjectives.add(chopper.next());
                chopper.nextLine();
            }
            chopper.close();
        }
        catch(Exception e) {}
    }

    public String getRandomVerb() {

        String verb = "";
        int num = 0;
        num = (int)(Math.random() * (verbs.size() - 1));
        verb = verbs.get(num);
        return verb;
    }

    public String getRandomNoun() {
        String noun = "";
        int num = 0;
        if (nouns == null) {
            loadNouns();
        }
        double rand = (Math.random());
        num = (int)(rand * (nouns.size() - 1));
        out.println(num);
        noun = nouns.get((int) num);
        out.print(noun);
        return noun;
    }

    public String getRandomAdjective() {
        String adj = "";
        int num = 0;
        num = (int)(Math.random() * (adjectives.size() - 1));
        adj = adjectives.get(num);
        return adj;
    }

    public String toString() {
        String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
        return output;
    }
}
  • 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-17T02:50:11+00:00Added an answer on June 17, 2026 at 2:50 am

    You are creating an instance of MadLib, then printing the object in a println in your Runner class…

         //make a new MadLib
         MadLib fun = new MadLib();
         out.println(fun);
    

    The out.println calls the toString() method you overrode in MadLib…

        String output = "The " + getRandomNoun() + getRandomVerb() + " after the " + getRandomAdjective() + getRandomAdjective() + getRandomNoun() + " while the " + getRandomNoun() + getRandomVerb() + " the " + getRandomNoun();
        return output;
    

    Your MadLib object has 3 ArrayLists you never initialized, so they are null…

    private ArrayList<String> verbs;
    private ArrayList<String> nouns;
    private ArrayList<String> adjectives
    

    The easiest way to fix the NullPointerException is to initialize the variables….

    private ArrayList<String> verbs = new ArrayList<String>();
    private ArrayList<String> nouns = new ArrayList<String>();
    private ArrayList<String> adjectives = new ArrayList<String>();
    

    However, what I really think you want to do is load all the nouns, verbs and adjectives when your object is constructed so your toString actually prints something useful. I’d add this to your constructor as well…

    public MadLib() {
      loadNouns();
      loadVerbs();
      loadAdjectives();
    }
    

    Edit: Your getRandom methods need to check if the list is empty to avoid the IndexOutOfBounds exception as well…

    public String getRandomVerb() {
        String verb = "";
    
        if (!verbs.isEmpty()) {
            int num = (int) (Math.random() * verbs.size() - 1);
            verb = verbs.get(num);
        }
    
        return verb;
    }
    
    public String getRandomNoun() {
        String noun = "";
    
        if (!nouns.isEmpty()) {
            int num = (int) (Math.random() * nouns.size() - 1);
            noun = nouns.get(num);
        }
    
        return noun;
    }
    
    public String getRandomAdjective() {
        String adj = "";
    
        if (!adjectives.isEmpty()) {
            int num = (int) (Math.random() * adjectives.size());
            adj = adjectives.get(num);
        }
    
        return adj;
    }
    

    Hope that helps

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

Sidebar

Related Questions

I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
this is what i have right now Drawing an RSS feed into the php,
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.