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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:35:12+00:00 2026-05-27T16:35:12+00:00

I am getting a weird NullPointerExcpetion in line 20: regs[Integer.parseInt(str.split( )[1]) – 1].add(line.poll()); I

  • 0

I am getting a weird NullPointerExcpetion in line 20:

regs[Integer.parseInt(str.split(" ")[1]) - 1].add(line.poll());

I do not know what has caused this. Can someone please help me fix this?

import java.io.*;
import java.util.*;

public class shoppay
{
public static void main (String[] args) throws IOException
{
    BufferedReader f = new BufferedReader(new FileReader("shoppay.in"));
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("shoppay.out")));
    Queue<Integer> line = new LinkedList <Integer>();
    int num = Integer.parseInt(f.readLine());
    String str;
    LinkedList<Integer>[] regs = (LinkedList<Integer>[]) new LinkedList[num];

    while ((str = f.readLine()) != null)
    {
        if (str.charAt(0) == 'C')
            line.add(Integer.parseInt(str.split(" ")[1]));
        else
            regs[Integer.parseInt(str.split(" ")[1]) - 1].add(line.poll());
    }

    out.close();
    System.exit(0);
}
}

Also, I am getting a warning:

Type safety: Unchecked cast from java.util.LinkedList[] to java.util.LinkedList[]

Does this have something to do with the error?

Edit: Input is just a string of lines. First line is a number, and the rest are either “C” or “R” followed by a number. Also, I need a queue for regs.

  • 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-27T16:35:13+00:00Added an answer on May 27, 2026 at 4:35 pm

    Don’t create an array of generic lists. For technical reasons, it doesn’t really work cleanly. It’s much better to use a list of lists:

    BufferedReader f = new BufferedReader(new FileReader("shoppay.in"));
    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("shoppay.out")));
    Queue<Integer> line = new LinkedList <Integer>();
    int num = Integer.parseInt(f.readLine()); // not needed
    String str;
    List<List<Integer>> regs = new ArrayList<List<Integer>>(num);
    
    for (int i = 0; i < num; ++i) {
        regs.add(new LinkedList<Integer>());
    }
    
    while ((str = f.readLine()) != null)
    {
        if (str.charAt(0) == 'C')
            line.add(Integer.parseInt(str.split(" ")[1]));
        else
            regs.get(Integer.parseInt(str.split(" ")[1]) - 1).add(line.poll());
    }
    

    As a side issue, is there any reason you’re using LinkedList for regs instead of ArrayList?

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

Sidebar

Related Questions

We are getting a weird issue on which we are not sure what exactly
Hi I'm getting nullpointerexception at rs.next() or rs.getString(1) it is really weird that sometimes
I'm getting weird results from the System.out.print immediately below the Scanner declaration in the
I'm getting weird EXC_BAD_ACCESS errors. I thought that I didn't need to retain objects
I'm getting some weird behaviour recompiling some applications in 2009 that used widestrings at
I'm getting a weird problem in GlassFish 2.1 - I'm a beginner with this
I'm getting some weird black spots with a custom map page (via the Google
I'm getting a weird instance of a NameError when attempting to use a class
I'm working on a school project and I'm getting some weird errors from Xcode.
When changing the selected item in a ListBox, I'm getting a weird error where

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.