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

  • SEARCH
  • Home
  • 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 8741051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:14:23+00:00 2026-06-13T11:14:23+00:00

I have a class inside the main class named Prims and a priority queue

  • 0

I have a class inside the main class named Prims and a priority queue of the class type.After I created an instance of the class with its construct, I want to push the object into the queue.Compiled well, But it shows run-time error with NullPointerException .Here is the code:

package mst.prims;

import java.util.*;

    public class Main {

        /**
         * Minimum Spanning Tree - Prim's Algorithm
         * @author Kaidul
         */
        static final int MAX = 100; 

        static class Prims{
            int u, v, cost;
            Prims(int u, int v, int cost){
                this.u = u;
                this.v = v;
                this.cost = cost;
            }
        }

        static PriorityQueue<Prims> q, q1, q2 = new PriorityQueue<Prims>(MAX);

        public static void main(String[] args)  {


            Scanner input = new Scanner(System.in);

            for (int i = 0; i < 7; i++) {
                int u, v, cost;
                u = input.nextInt();
                v = input.nextInt();
                cost = input.nextInt();
                Prims temp = new Prims(u, v, cost);

                q.add(temp);
            }
        }

    }

Error :

1 2
2 3
Exception in thread "main" java.lang.NullPointerException
    at mst.prims.Main.main(Main.java:36)

I am new in Java and can’t fix it.

  • 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-13T11:14:24+00:00Added an answer on June 13, 2026 at 11:14 am

    From the Javadoc for PriorityQueue:

    A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

    You must implement Comparable in Prims. If you intend to use Prims in collections you should also override equals() and hashCode() with implementations appropriate for your object.

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

Sidebar

Related Questions

I have a method inside of a MooTools class that I want to access
i have a class that uses a proxy class inside to call a service
So I have a class with a wrapper class inside like so: public class
Assume the following: we have class B, which is a private class nested inside
I have an XML class inside a .NET 3.5 project which is the top
I have an anonymous inner class inside another class ( SomeClass ). Both SomeClass.class.getClasses()
I have a class with an NSDictionary attribute. Inside this class I dispatch another
At the moment inside the class I have ArrayList which the objects are stored
I have a class that holds another objects inside it (List, Set and objects
I have a class level price variable decalred inside a page, like this: public

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.