0

I´m writing an internet radio application. I´m running a JavaZoom Player in one thread but I can´t close the player from the main frame. I always get an NullPointerException. Here is the code of the Player :

import java.net.*;
import java.util.Scanner;
import java.io.*;
import java.net.*;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import javax.*;
import javax.swing.ImageIcon;

public class Main extends Thread {
    public static URL radiourl;
    public Player play;
    public static int art = 0;
    public static String radio = null;
    InputStream stream;

    public void run() {
        int valuehere = RadioGui.value;
        switch (valuehere) {
        case 1:
            radio = "http://mp3-live.swr3.de/swr3_m.m3u";
            art = 1;
            break;
        case 2:
            radio = "http://www.wdr.de/wdrlive/media/einslivedigi.m3u";
            art = 1;
            break;
        case 3:
            radio = "http://www.wdr.de/wdrlive/media/wdr2.m3u";
            art = 1;
            break;
        case 4:
            radio = "http://sunshinelive.hoerradar.de/sunshinelive-live-mp3-hq";
            art = 2;
            break;
        default:
        }
        if (art == 1) {
            try {
                URL url = new URL(radio);
                System.out.println(url);
                Scanner scan = new Scanner(url.openStream());
                String radiourl;
                String radioendurl = null;
                while (scan.hasNext()) {
                    radiourl = scan.nextLine();
                    if (radiourl.contains(".mp3")) {
                        radioendurl = radiourl;
                    }
                }
                url = new URL(radioendurl);
                stream = url.openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (art == 2) {
            try {
                URL url = new URL(radio);
                stream = url.openStream();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            play = new Player(stream);
            RadioGui.isRunning = true;
            System.out.println(RadioGui.isRunning);
            play.play();
        } catch (JavaLayerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

In the real Main(yes I know this class is called Main but it isnt the real Main...) I cant close the player(When I type m(Main).play.close();. The code that causes the error:

label.addMouseListener(new MouseAdapter() {
        @SuppressWarnings("deprecation")
        @Override
        public void mouseClicked(MouseEvent arg0) {
            sendername = (String)sender.getSelectedItem();
            if(sendername.equals("SWR 3")){
                value = 1;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/swr 3.png")));
                name.setText(sendername);
            }
            if(sendername.equals("1 Live DIGGi")){
                value = 2;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/1livediggi.png")));
                name.setText(sendername);
            }
            if(sendername.equals("WDR 2")){
                value = 3;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/wdr2.gif")));
                name.setText(sendername);
            }
            if(sendername.equals("SUNSHINE LIVE")){
                value = 4;
                bild.setIcon(new ImageIcon(RadioGui.class.getResource("/Images/sunshine.png")));
                name.setText(sendername);
            }
            Main m = new Main();
            Thread y = new Thread(m);
            if(isRunning==true){
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {

                }
                y.stop();
                m.play.close();//Here is the error
                System.out.println(y.isAlive());
                System.out.println(m.play.isComplete());
                isRunning = false;
                y.start();
            }
            else{
            y.start();
            }
        }
    });
Jonas
  • 121,568
  • 97
  • 310
  • 388
Nireki
  • 1
  • 2
  • What line in your code above throws the NPE? Where is your stacktrace? Also, have you searched on this exception and reviewed this decent answers to be found in this similar question: [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Hovercraft Full Of Eels Jan 30 '18 at 13:29
  • The line "m.play.close()" Causes the exception – Nireki Jan 30 '18 at 13:45
  • 1
    Which is code you've not posted above -- How can we understand what is causing bugs in code not shown? You need to show that code and its context. – Hovercraft Full Of Eels Jan 30 '18 at 13:49
  • Also, what variable on that line is null? m? play? Have you tested this? Have you used a debugger? – Hovercraft Full Of Eels Jan 30 '18 at 13:52
  • Sorry, i´ve edited it now! – Nireki Jan 30 '18 at 13:52
  • You're calling close on play before starting the thread. The object is created within the run method, so it is null when you close it. – Hovercraft Full Of Eels Jan 30 '18 at 13:53
  • But the thread is already started! At the first click is only y.start(); but then isRunning is set to true so at the second click the threat gets stopped. – Nireki Jan 30 '18 at 13:59
  • 1
    That doesn't matter at all since you're creating a completely new Main object since you're using local variables. Sorry but this code has many issues, and if this were my project, I'd consider starting over, refactoring the non-GUI elements out of the GUI. You're extending Thread for instance, using the `@SuppressWarnings("deprecation")` annotation (!), and most important for your purposes, using local variables where fields should be used. Consider refactoring this so that it's much cleaner and easier to debug and enhance. – Hovercraft Full Of Eels Jan 30 '18 at 14:00
  • Every time this MouseListener code is called, a new Main object is created. And so even if you've got the thread running, you're calling close on a Thread that hasn't been started. Again, refactor, simplify, use first principles. – Hovercraft Full Of Eels Jan 30 '18 at 14:02
  • Ohh okay. I will try to fix this – Nireki Jan 30 '18 at 14:04
  • Okay now I´m getting an IllegalThreadStateException when I try to restart the Thread with y.start() ( after closing the Player, this is working now, thank you!) – Nireki Jan 30 '18 at 14:06
  • Okay I´ve found the soloution myself :D I simply create a new Thread after stopping the old one! Thank you for your help !! – Nireki Jan 30 '18 at 14:13

0 Answers0