Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: How do I print this?

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default How do I print this?

    Hello,

    Can someone help me with this piece of coding?

    I have a class Workgroep. In this class there is a piece of coding where you can connect a workgroup together with a workgroup. This works and it connects the workgroups together. However, it only has a reference that it is connected to one of the instances. So for example, I created 2 workgroups named, workgroup1 and workgroup2. If I connect workgroup2 to workgroup1, it only creates a connection in workgroup1.

    How would I fix that in this piece of coding, so that both workgroups have a connection to each other.

    public void werkgroepKoppelen (Werkgroep werkgroep)
        {
            if (gekoppeldeWerkgroep == null && isGekoppeld == false)
            {
                gekoppeldeWerkgroep = werkgroep;
                isGekoppeld = true;
                werkgroep.isGekoppeld = true;
            }
        }

    This brings me to the next point. I created a print function, where the workgroup information is printed into the terminal page. Continuiing with the above example, where workgroup2 is connected to workgroup1. If I print this for the instance workgroup1, it works and it prints all the information. However, if I try to print this for workgroup2 it creates this error: java.lang.NullPointerException in this piece of coding.

    I know this has to do with workgroup2 not having a reference that it workgroup1 is connected to it. But I don't know how to add that in coding.

                info += "De werkgroep " + gekoppeldeWerkgroep.naam + " kent de volgende deelnemers: " + "\n"

    public void printWerkgroep()
        {
            String info = "De naam van de werkgroep is: " + naam + "\n";
            info += "De werkgroep " + naam + " kent de volgende deelnemers:" + "\n";
     
            if (werkverdeler != null)
            {
                info += (werkverdeler.studentRol()) + "\n";
            }
            if (theoreticus != null)
            {
                info += (theoreticus.studentRol()) + "\n";
            }
            if (practicus != null)
            {
                info += (practicus.studentRol()) + "\n";
            }
            if (schrijver != null)
            {
                info += (schrijver.studentRol()) + "\n";
            }
     
            if (isGekoppeld != false)
            {
                info += "De werkgroep " + naam + " is samengevoeg met de werkgroep " + gekoppeldeWerkgroep.naam + ".\n";
                info += "De werkgroep " + gekoppeldeWerkgroep.naam + " kent de volgende deelnemers: " + "\n";
                if (gekoppeldeWerkgroep.werkverdeler != null)
                {
                    info += (gekoppeldeWerkgroep.werkverdeler.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.theoreticus != null)
                {
                    info += (gekoppeldeWerkgroep.theoreticus.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.practicus != null)
                {
                    info += (gekoppeldeWerkgroep.practicus.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.schrijver != null)
                {
                    info += (gekoppeldeWerkgroep.schrijver.studentRol()) + "\n";
                }
            }
            else 
            {
                info += "De werkgroep " + naam + " is niet gekoppeld aan een andere werkgroep";
            }
     
            System.out.println(info);
        }

    Thanks for the help!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I print this?

    connects the workgroups together.
    Can you explain what it means to have workgroups connected? What is the code that does that?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    19
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: How do I print this?

    Can you explain what it means to have workgroups connected? What is the code that does that?

    Here is the entire code of the class Workgroup. I used the naming in Dutch, as it is a Dutch assignment, and added English Notes where I get stuck. I hope this is clear, if not let me know!

    Thanks!

    public class Werkgroep
    {
        /**
         * Variables of the class Werkgroep.
         */
        private String naam;
        private double gemiddeldCijfer; 
        private Student werkverdeler; 
        private Student theoreticus;
        private Student practicus; 
        private Student schrijver;
        private boolean isGekoppeld; //isGekoppeld means isConnected. If an object in this class is created, it is on False - Not connected.
        private Werkgroep gekoppeldeWerkgroep; //gekoppeldeWerkgroep means connectedWorkgroup. This is set on NULL when creating an object. In the method 'public void werkgroepKoppelen (Werkgroep werkgroep)' I connect 2 objects to eachother. However, only one object changes from NULL to a reference. (If that makes sense). 
     
        /**
         * De constructor van de klasse Werkgroep.
         * 
         * @param   naam    De naam van de werkgroep.
         * @param   double  Het gemiddelde cijfer wat nodig is om toegelaten te worden aan de werkgroep.
         */
        public Werkgroep(String naam, double gemiddeldCijfer)
        {
            this.naam = naam;
            this.gemiddeldCijfer = gemiddeldCijfer;
            isGekoppeld = false;
            werkverdeler = null;
            theoreticus = null;
            practicus = null;
            schrijver = null;
            gekoppeldeWerkgroep = null;
        }
     
        public void voegStudentToe  (Student student) //This is where you helped me with in my last post. Thanks for that still :)
        {
            switch(student.getRol())
            {
                case "Werkverdeler":
                    if (student.getGemiddeldCijfer() >= gemiddeldCijfer && student.eerdereDeelname() == false && werkverdeler == null)
                    {
                        werkverdeler = student;
                        student.eerdereDeelname = true; 
                    }
                    break;
                case "Theoreticus":
                    if (student.getGemiddeldCijfer() >= gemiddeldCijfer && student.eerdereDeelname() == false && theoreticus == null)
                    {
                        theoreticus = student;
                        student.eerdereDeelname = true; 
                    }
                    break;
                case "Practicus":
                    if (student.getGemiddeldCijfer() >= gemiddeldCijfer && student.eerdereDeelname() == false && practicus == null)
                    {
                        practicus = student;
                        student.eerdereDeelname = true; 
                    }
                    break;
                case "Schrijver":
                    if (student.getGemiddeldCijfer() >= gemiddeldCijfer && student.eerdereDeelname() == false && schrijver == null)
                    {
                        schrijver = student;
                        student.eerdereDeelname = true; 
                    }
                    break;
            }
     
     
        }       
     
        public void verwijderStudent (Student student)
        {
            if(student.getRol().equals("Werkverdeler"))
            {
                werkverdeler = null;
            }
            else if (student.getRol().equals("Theoreticus"))
            {
                theoreticus = null;
            }
            else if (student.getRol().equals("Practicus"))
            {
                practicus = null;
            }
            else if (student.getRol().equals("Schrijver"))
            {
                schrijver = null;
            }
        }
     
     
        public void werkgroepKoppelen (Werkgroep werkgroep) //this is the code where the objects of the class Werkgroep are connected. In an example: if I create 2 objects, naming Workgroup1 and Workgroup2. If I put in the paramater here the Workgroup2, then only Workgroup1 gets that the variable gekoppeldeWerkgroep is not NULL anymore. And I want that Workgroup 2 is not NULL, but has a connection to Workgroup1. How would that translate in code? 
        {
            if (gekoppeldeWerkgroep == null && isGekoppeld == false)
            {
                gekoppeldeWerkgroep = werkgroep;
                isGekoppeld = true;
                werkgroep.isGekoppeld = true;
            }
        }
     
        /**
         * Methode - een werkgroep ontkoppelen van een andere werkgroep.
         * @param   werkgroep   De werkgroep die ontkoppeld wordt. 
         */
        public void werkgroepOntkoppelen (Werkgroep werkgroep)
        {
            gekoppeldeWerkgroep = null;
            isGekoppeld = false;
            werkgroep.isGekoppeld = false;
        }
     
        /**
         * Accessormethode
         * 
         * Geeft terug of een werkgroep is gekoppeld.
         * @return  isGekoppeld true = werkgroep is gekoppeld, false = werkgroep is niet gekoppeld.
         */
        public boolean getIsGekoppeld()
        {
            return isGekoppeld;
        }
     
        /**
         * Methode - Print werkgroep informatie.
         */
        public void printWerkgroep()
        {
            String info = "De naam van de werkgroep is: " + naam + "\n";
            info += "De werkgroep " + naam + " kent de volgende deelnemers:" + "\n";
     
            if (werkverdeler != null)
            {
                info += (werkverdeler.studentRol()) + "\n";
            }
            if (theoreticus != null)
            {
                info += (theoreticus.studentRol()) + "\n";
            }
            if (practicus != null)
            {
                info += (practicus.studentRol()) + "\n";
            }
            if (schrijver != null)
            {
                info += (schrijver.studentRol()) + "\n";
            }
     
            if (isGekoppeld != false)
            {
                info += "De werkgroep " + naam + " is samengevoeg met de werkgroep " + gekoppeldeWerkgroep.naam + ".\n";
                info += "De werkgroep " + gekoppeldeWerkgroep.naam + " kent de volgende deelnemers: " + "\n"; //if I try to run this in the object Workgroup2, I get the error java.lang.NullPointerException. Because in the object of Workgroup2, the variable gekoppeldeWerkgroep is still on NULL. 
                if (gekoppeldeWerkgroep.werkverdeler != null)
                {
                    info += (gekoppeldeWerkgroep.werkverdeler.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.theoreticus != null)
                {
                    info += (gekoppeldeWerkgroep.theoreticus.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.practicus != null)
                {
                    info += (gekoppeldeWerkgroep.practicus.studentRol()) + "\n";
                }
                if (gekoppeldeWerkgroep.schrijver != null)
                {
                    info += (gekoppeldeWerkgroep.schrijver.studentRol()) + "\n";
                }
            }
            else 
            {
                info += "De werkgroep " + naam + " is niet gekoppeld aan een andere werkgroep";
            }
     
            System.out.println(info);
        }
     
    }


    --- Update ---

    Hi Norm,

    Thanks, I figured it out! It should be:

    werkgroep.gekoppeldeWerkgroep = this;
    Last edited by F_VRBRG; February 8th, 2022 at 07:34 AM.

Similar Threads

  1. Print help
    By DiogoPierces in forum Java Theory & Questions
    Replies: 1
    Last Post: June 22nd, 2020, 03:33 PM
  2. How to print?
    By vijay$ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 9th, 2014, 04:05 PM
  3. Replies: 1
    Last Post: December 3rd, 2012, 02:35 PM
  4. xfa.host.print: print a page + some page range.
    By gammaman in forum Totally Off Topic
    Replies: 2
    Last Post: May 10th, 2012, 08:07 AM
  5. how can i print the out put !
    By Faha in forum Java IDEs
    Replies: 5
    Last Post: December 30th, 2010, 04:41 AM