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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:08:07+00:00 2026-06-10T07:08:07+00:00

My code is basically a program to check whether it is possible to use

  • 0

My code is basically a program to check whether it is possible to use the ordered sequence 1,2,3..,n to generate a permutation of this sequence as specified by the user using a stack as temporary storage structure. The user has the option of entering n and the permutation he’d like to see generated using 2 methods; either through a text file or directly in the command line.
So, for example if the user enters 5 1 3 5 4 2, the n would be interpreted as the first number, i.e. 5 and then the rest of the digits is the permutation he’d like to see if it’s possible to generate starting from 1 2 3 4 5 (Note that 1 2 3 4 5 is ordered and 1 is at the top of that stack). Here you’d have 1 being used directly, then 2 being stored in a stack, then 3 being used, then 4 stored on top of 2, then 5 being used, then 4 popped out followed by 2 being popped to generate the permutation.
The problem I’m having is my program faces a NullPointerException whenever I try to generate the starting stack of 1 2 3 … n. It points to the last line of this block of code:

public static void main(String args[])
{
    int[] arr;
    arr = null;
       try
       {
        if(args[0].charAt(0) == '2') 
        {   
            try
            {   
                FileInputStream file = new FileInputStream(args[1]);
                arr = input(file);
            }
            catch (FileNotFoundException e)
            {
                System.out.println("File not found.");
                System.exit(0);
            }
        }
        else if (args[0].charAt(0) == '1')
        {   
            arr = input();
        }   
           else 
        {
            System.out.println("Please enter a valid input option.");
            System.exit(0);
        }
    }
    catch (ArrayIndexOutOfBoundsException e)
    {
        System.out.println("Please enter a valid input option."); 
        System.exit(0);
     }
    int x;
    x = arr.length;
    System.out.println(x);
    ArrayPerm start = new ArrayPerm(x);
    ArrayPerm temp = new ArrayPerm(x);
    for (int i = 0; i < x; i++)
    {
        *start.push(x - i);*        
    }

And it also points to:

public void push(int j)
{
    top++;
    Stack[top] = j;
}

The ArrayPerm class is basically the stack implementation.
I tried doing this:

public void push(Integer j)
{
    if (j == null)
    {
        throw new NullPointerException("NULL ELEMENT!");
    }
    else
    {
        top++;
        Stack[top] = j;
    }
}

But it’s still showing the exception. If anyone could point me in the right direction I’d really appreciate it. I’ve spent an hour looking for the problem in my code without result.
So, thanks in advance!

EDIT: This is how the class is defined, so Stack shouldn’t be initialized?

public class ArrayPerm
{
  private int[] Stack;
  private int top;
  public int size;

public ArrayPerm(int n)
{
    size = n;
    int[] Stack = new int[n];
    top = -1;
}
  • 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-10T07:08:09+00:00Added an answer on June 10, 2026 at 7:08 am

    You’re shadowing the variable Stack. Replace

    int[] Stack = new int[n];
    

    with

    stackArray = new int[n];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So this is driving me crazy. Basically, when I hard-code my user name and
I am using an example program from this code http://sicktoolbox.sourceforge.net/ > http://sourceforge.net/projects/sicktoolbox/files/ . It's
This code basically translates characters based on position in one string to the character
This is a follow up from my previous question I have this code basically
This is the first time I got this error. This code basically gets the
I have this horribly stripped delphi code that basically login to server, save cookie
I have this small piece of code that basically takes a list and runs
So basically this code: class A { }; class B { B (const B&
So basically this code was working fine before. I had some computer issues and
I am making a program using the LRV(Least recently visited) Algorithm. Basically, I design

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.