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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:26:45+00:00 2026-06-14T02:26:45+00:00

I am currently writing a command line program that is supposed to take in

  • 0

I am currently writing a command line program that is supposed to take in lines from a text file and add data to an array of HashMaps. I currently get a NullPointerException when running this method.

Public class Vaerdata {
String[] linje;
String line;
String line2;
HashMap<String, Stasjon> stasjonsmap = new HashMap<String, Stasjon>();
HashMap<String, Stasjon>[] regionmap = (HashMap<String, Stasjon>[]) new HashMap<?, ?>[6];


void init() throws IOException{
    BufferedReader br = new BufferedReader(new FileReader("stasjoner_norge.txt"));
    BufferedReader brData = new BufferedReader(new FileReader("klimadata2012.txt"));
    for(int i = 0; i < 10; i++){
        brData.readLine();
    }
    br.readLine();
    while((line = br.readLine()) != null){
        linje = line.split("\\s+");
        stasjonsmap.put(linje[1], new Stasjon(Integer.parseInt(linje[1]), linje[2], Integer.parseInt(linje[3]), linje[4], linje[5], linje[6]));
        }
        if(linje[6].equals("AGDER")){
            System.out.println(stasjonsmap.get(linje[1])); //DEBUG
            regionmap[1].put(stasjonsmap.get(linje[1]).navn, stasjonsmap.get(linje[1]));
            System.out.println(regionmap[1].get(stasjonsmap.get(linje[1]).navn)); //DEBUG
        }
    }  
}

My NullPointerException happens in this line:

regionmap[1].put(stasjonsmap.get(linje[1]).navn, stasjonsmap.get(linje[1]));

So my question is: When I have declared the array of HashMaps with <String, Stasjon> (Stasjon is an object of my Stasjon class, taking info about certain weather stations), why do I get an error in that line? The object in stasjonsmap.get(linje[1]) has been declared, and I can’t understand why it won’t allow me to make a reference to this object in my second hashmap.

Every line in the text file, after line one (which I skip in my program) look like this:
36200 TORUNGEN_FYR 12 ARENDAL AUST-AGDER AGDER

In advance; thanks for your help.

  • 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-14T02:26:46+00:00Added an answer on June 14, 2026 at 2:26 am

    When you initialize your array of HashMap here

    HashMap<String, Stasjon>[] regionmap = (HashMap<String, Stasjon>[]) new HashMap<?, ?>[6];

    all values in the array are null.

    You then try to call the put method of HashMap on a null-reference.

    First you have to initialize your HashMaps somehow:

    for (int i = 0; i < regionmap.length; i++) {
        regionmap[i] = new HashMap<String, Stasjon>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently writing a lightweight program that consolidates many command line and other external
I am writing a program to extract data from a bunch of text files
I'm currently writing a Perl program that is to read a given file (either
I am writing an command line application in C and from within the program,
On Windows 7, I have a command-line program that fails due to file write permission
I'm currently working on a small utility program that only requires a command line
I am currently writing a program using Weka that builds a model (using one
I'm currently writing a Powershell script that's used to select a user from Active
I am currently writting programs in linux like this: From the command line I
I'm writing a command-line tool (to run on Windows 7 boxes) that inserts a

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.