-1

Why does my if-else block not detect that gamma is null? Here is my code. It is a Spreadsheet application and this is the save function which loops through all of the cells in the table and writes it to a file. Here is my code:

public void Save () {
        String FileName = JOptionPane.showInputDialog("Please enter the name for your file:");

        if (FileName == null) {
            System.err.println("You didn't enter anything");
        } else {
            int rows = Table.getRowCount();
            int columns = Table.getColumnCount();
            int alpha = 0;
            int beta = 1; 
            choicer.setCurrentDirectory(new java.io.File("Desktop"));
            choicer.setDialogTitle("Save Document");
            choicer.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            choicer.setAcceptAllFileFilterUsed(false);
            if (choicer.showSaveDialog(new JPanel()) == JFileChooser.APPROVE_OPTION) {
                dir = String.valueOf(choicer.getSelectedFile());
            }
            File f = new File(dir + "\\" + FileName + ".txt");
            try {
                fos = new FileOutputStream(f);
                osw = new OutputStreamWriter(fos);
                w = new BufferedWriter(osw);
                for (alpha = 0; alpha <= rows - 1; alpha++) {
                    for (beta = 1; beta < columns; beta++) {
                        String gamma = String.valueOf(Table.getValueAt(alpha, beta));
                        if (gamma != null) {
                            w.write("<%! " + gamma + " !%> ");
                        } else {
                            w.write(" <%! |^-^| !%> ");
                        }                           
                    }
                    w.write("\n");
                }
            } catch (IOException e) {
                System.err.println("Some prob dude. Too big for me"); 
            } finally {
                try {
                    w.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

I tried the link given by the duplicate thing but that doesn't solve my problem. If I do if(gamma != null && !(gamma.isEmpty)) then only null is written to the file as the value of gamma.

Adit Kirtani
  • 113
  • 7

2 Answers2

0

Maybe, gamma is empty but not null:

Change

if (gamma != null)

To

if (gamma != null && !gamma.isEmpty())
guipivoto
  • 18,327
  • 9
  • 60
  • 75
0

There are only three things that I can think of that would cause this. I will get to those in a second. First, what IDE are you using? Do you have the ability to set a breakpoint and watch variables? This will help you in troubleshooting the following. 1. Can you ensure that the first for loop hits? 2. Then, make sure you are getting in the second for loop. 3. Finally, what is the actual value of the string gamma? This is where you need a breakpoint and to inspect that variable when you are expecting it to be null. Likely you will see it is not. That is of course, assuming that your for loops are even letting you get there.

Hope this helps!

Michael Bedford
  • 1,742
  • 20
  • 48