Q:

While loop not updating variable defined by user

So, I have two classes.
main class:
package guiprojj;
import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.Scanner;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import guiprojj.gui;

import javax.swing.JFrame;

@SuppressWarnings("unused")
public class Test {
    public static String movie;
    public static String line;
    public static void main(String args[]) throws IOException {
        BufferedReader rd;
        OutputStreamWriter wr;
        //Scanner s = new Scanner(System.in);
        //System.out.println("Enter input:");
        //movie = s.nextLine();
        //movie = movie.replaceAll(" ", "%20");
        while (movie != null)
        {
            try {
                URL url = new URL("http://www.imdbapi.com/?i=&t=" + movie);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.flush();

                // Get the response
                rd = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                line = rd.readLine();
                if (line != null) {
                    System.out.println(line);
                } else {

                    System.out.println("Sorry! That's not a valid URL.");
                }
            } catch (UnknownHostException codeyellow) {
                System.err.println("Caught UnknownHostException: " + codeyellow.getMessage());
            }
            catch (IOException e)
            {
                System.out.println("Caught IOException:" + e.getMessage());
            }

        }
    }
}

gui class:
package guiprojj;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class gui {
    public static void main(String[] args)
    {
        JFrame maingui = new JFrame("Gui");
        JPanel pangui = new JPanel();
        JButton enter = new JButton("Enter");
        JLabel movieinfo = new JLabel(Test.line);
        final JTextField movietext = new JTextField(16);
        maingui.add(pangui);
        pangui.add(movietext);
        pangui.add(enter);
        pangui.add (movieinfo);
        maingui.setVisible(true);
        maingui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        maingui.pack();
        enter.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e)  
            {
                Test.movie = movietext.getText();
                System.out.println(Test.movie);

            }
            });
        }
}

What I am writing is a program that outputs the movie data from imbd after you enter it in the box, and I am running into an issue.
When I type in the movie, and press enter, it still shows as null and doesn't seem to be outputting the data from the api I am using.

A:

public class Test {

    public static String getMovieInfo(String movie) {
        BufferedReader rd;
        OutputStreamWriter wr;
        //Scanner s = new Scanner(System.in);
        //System.out.println("Enter input:");
        //movie = s.nextLine();
        //movie = movie.replaceAll(" ", "%20");
        if (movie != null)
        {
            try {
                URL url = new URL("http://www.imdbapi.com/?i=&t=" + movie);
                URLConnection conn = url.openConnection();
                conn.setDoOutput(true);
                wr = new OutputStreamWriter(conn.getOutputStream());
                wr.flush();

                // Get the response
                rd = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String line = rd.readLine();
                if (line != null) {
                   return line;
                } else {

                    return "Sorry! That's not a valid URL.";
                }
            } catch (UnknownHostException codeyellow) {
                System.err.println("Caught UnknownHostException: " + codeyellow.getMessage());
            }
            catch (IOException e)
            {
                System.out.println("Caught IOException:" + e.getMessage());
            }

        }
        else
        {
            return "passed parameter is null!";
        }

        return "an error occured, see console!";
    }
}

I've rewritten your Test-class. Renamed main-method, added return-statements (String) and removed the while-loop. Try to avoid multiple main(String[] args)-methods in one project, else your IDE could launch the "wrong" one, if your context switches.
I did not test it, but now you should be able to call Test.getMovieInfo("movie-name") from your gui-class to get the info.
(nethertheless this code should be refactored ;))

