r/javahelp • • Aug 23 '22

I get "java.lang.NullPointerException" on my program and I don´t know why

0 Upvotes

Hi

My program compiles and it works almost perfectly. The only error I get comes when I try to enter a number (double) in this field.

if(indice>= 0)
{
                                            
 System.out.println("============================================");
     System.out.println("Presupuesto:");
     presupuesto = sc.nextDouble();
                                            
     if(presupuesto > 0)
     {
                                                        
      contable.setPresupuestoProyecto(presupuesto);
     }else
     {
          System.out.println("Por favor asigne un presupuesto válido");
          exit = true;
     }
     exit = false;
}

For context, I scan a double type nomber to set a price for a project. The method "contable.setPresupuestoProyecto(presupuesto)" is already defined in class "Contable" and all.

public void setPresupuestoProyecto(double presupuesto)
    {
        this.proyecto.setPresupuesto(this.presupuesto);
        presupuestoAsignado = true;
        this.proyecto.setPresupuestoAsignado(presupuestoAsignado);
    }

this would be the method. I get "java.lang.NullPointerException" in console every time I try to enter a price on this field. What am I missing?


r/javahelp • • Aug 31 '22

Unsolved Java Static

0 Upvotes

Why cannot we use System.out.println(), outside <main()> or any static function ?


r/javahelp • • Aug 29 '24

Solved How to return a String as JSON from a @RestController

0 Upvotes

I'm using Spring Boot 3, I would like to return a String from an endpoint, not a Java object, I put the @RequestMapping annotation with produces = MediaType.APPLICATION_JSON_VALUE, the content type is set succesfully but the string is not escaped and the browser marks it as invalid json, it does not marshal it.

The RestController:

package com.example.api.controllers;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello World!!!";
    }

}

The idea is that Spring should marshal the string Hello World!!! to its JSON representation: "Hello World!!!" with double quotation marks


r/javahelp • • Oct 09 '24

Help making a program

0 Upvotes

Hello, I'm an amateur doing java I need help trying to do the following code using the program NETBEANS. I am not sure where to start without using buttons, I would really appreciate if someone would point me on a direction on how to start. The program must include the following:

At least 5 multiple choice questions
The ability for the user to enter an answer for each question
Feedback related to whether the user’s answer was correct
The ability to read in uppercase or lowercase responses from the user (you might want to use the &&, AND, Boolean logic operator for this)
A score counter that increases each time a question is answered correctly
Statistics at the end of the quiz that provides:
The number of questions answered correctly
The number of questions answered incorrectly
The percentage of questions answered correctly, rounded to one decimal place.
A user-friendly interface that implements a GUI
Appropriately named components, variables and constants
Commenting and appropriate spacing and indenting
Constants for all values that will not change as the program is run

Note: In your multiple choice quiz, you may want the user to enter in a letter as their answer.


r/javahelp • • Aug 25 '15

what are the errors in this?

0 Upvotes
 public class WorkingWeek {
     public static void main (String[] args){

    // setting the values
    double hoursPerDay = 7.5;
    int workdaysWeek = 5;

    // calculating the total amout on wokring hours per week
    double HoursPerWeek = HoursPerDay * WorkdaysWeek ;

    // the results
    System.out.println HoursPerDay (" workingHours ")  + 
    DaysWeek + (" DaysWeek ") +
    HoursWeek + (" HoursWeek ") {
      
 }

}

using drjava- and how to make it calculate/Write how many hours it is in 4 weeks/a month?

thanks!


r/javahelp • • Aug 25 '15

Creating installers: How do I do it? (Mac and PC)

0 Upvotes

So, I have my jar all ready to go but I want to create installers with a JRE, so it can be run on other computers that don't java the JDK, etc.

I have PackageMaker for Mac and I can get softs for Windows but I want to know how I put a JRE in there and how I tell it to run the jar using the JRE.

Maybe I'm coming at this from totally the wrong direction, I don't know.

The ultimate goal is to get the jar to run on any computer with minimum fuss for the end user.

Thanks!


r/javahelp • • Dec 03 '14

Gangnam Style and 32bit Integer

0 Upvotes

Yes, YouTube video view count is limited to a 32 bit Integer. Gangnam Style has more so YouTube will fix it. Channel views do not have this problem.

I have a question. Is there 64bit integers available to programmers using 64bit architecture? If not, why not?

Thanks.


r/javahelp • • Jun 08 '15

i am getting an error message when i multiply a number can anyone help me?

0 Upvotes
 public class lala extends JFrame{

//Creating Instance Variables
private JButton b0;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JButton b6;
private JButton b7;
private JButton b8;
private JButton b9;
private JButton clear;
private JButton del;
private JButton add;
private JButton subtract;
private JButton divide;
private JButton multiply;
private JButton dot;
private JButton enter;
private JTextField field;
private JPanel panel;
private double temp;
private double temp1;

public lala(){
	
	//Making objects for Components
	b0 = new JButton("0");
	b1 = new JButton("1");
	b2 = new JButton("2");
	b3 = new JButton("3");
	b4 = new JButton("4");
	b5 = new JButton("5");
	b6 = new JButton("6");
	b7 = new JButton("7");
	b8 = new JButton("8");
	b9 = new JButton("9");
	del = new JButton("del");
	clear = new JButton("C");
	panel = new JPanel();
	field = new JTextField();
	add = new JButton("+");
	subtract = new JButton("-");
	multiply = new JButton("*");
	divide = new JButton("/");
	dot = new JButton(".");
	enter = new JButton("Enter");
 
	//Setting color to Panel
	panel.setBackground(Color.GRAY);
	
	//Setting Layout To Editors Choice 
	panel.setLayout(null);
	
	//Editing Fields
	field.setOpaque(false);
	field.setFont(new Font("serif",Font.ITALIC,40));
	
	//Setting the  Components Positions
	enter.setBounds(184,217,106,43);
	subtract.setBounds(247,170,43,43);
	add.setBounds(184,170,60,43);
	divide.setBounds(248,122,43,44);
	multiply.setBounds(184,122,60,43);
	dot.setBounds(0,0,0,0);
	del.setBounds(184,76,60,43);
	field.setBounds(3,3,289,70);
	clear.setBounds(248,76,43,43);
	b0.setBounds(3,218,136,41);
	b1.setBounds(3,170,43,43);
	b2.setBounds(49,170,43,43);
	b3.setBounds(95,170,43,43);
	b4.setBounds(3,123,43,43);
	b5.setBounds(49,123,43,43);
	b6.setBounds(95,123,43,43);
	b7.setBounds(3,76,43,43);
	b8.setBounds(49,76,43,43);
	b9.setBounds(95,76,43,43);
	
	//Adding Listeners to Buttons

     enter.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			
			temp = Integer.parseInt(field.getText());
			temp1 = Integer.parseInt(field.getText());
	        field.setText(Double.toString(temp1*temp));
	        
		}
});
	dot.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
		field.setText(field.getText()+".");
	}
});
	multiply.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			field.setText("*");
	}
});
	divide.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			field.setText(field.getText()+"/");
	}
});
	subtract.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			field.setText("-");
	} 
});
	add.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent e) {
			field.setText("+");
			
	}
});
	del.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText("");
	}
});
	clear.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(null);
	}
});
	b0.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"0");
	}
});
	b1.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"1");	
	}
});
	b2.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"2");	
	}
});
	b3.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"3");	
	}
});
	b4.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"4");
	}
});
	b5.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"5");	
	}
});
	b6.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"6");	
	}
});
	b7.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"7");	
	}
});
	b8.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"8");	
	}
});
	b9.addActionListener(new ActionListener(){
		public void actionPerformed(ActionEvent arg0) {
		field.setText(field.getText()+"9");	
	}
});
	
	
	//Adding Components to Panel
	panel.add(enter);
	panel.add(add);
	panel.add(subtract);
	panel.add(multiply);
	panel.add(divide);
	panel.add(dot);
	panel.add(del);
	panel.add(clear);
	panel.add(field);
	panel.add(b0);
	panel.add(b1);
	panel.add(b2);
	panel.add(b3);
	panel.add(b4);
	panel.add(b5);
	panel.add(b6);
	panel.add(b7);
	panel.add(b8);
	panel.add(b9);
	
	//Adding Panel to JFrame
	add(panel);
	
}

}

r/javahelp • • Jun 21 '17

how to generate unique , random numbers for invoiceNumber in java ? what is the simplest solution

0 Upvotes

does anybody know how to create an invoice number in java ?


r/javahelp • • Aug 30 '18

Homework Creating class files

0 Upvotes

Hey, I'm a first year at uni doing a programming class and I'm having some trouble with some work. I have no idea what to do to be honest, I haven't learnt anything about coding and am just stuck. I understand how class's work as well as attributes and methods, I just don't know how to write the code itself. The work also comes with a prewritten class file and class marks, I don't know what to do with them either. Any help would be appreciated, I have linked below the link to pics of the work.

https://imgur.com/a/HZc7Fqt


r/javahelp • • Mar 11 '26

How to output a table using Prepared Statement JDBC?

0 Upvotes

I am trying to output a table with prepared statement so that I can also output linked data from another table but It does not work. I have copy pasted working lines of code from other areas and have only changed the names of variables and my function things where appropriate. My SQL statement is accurate, so I ma not sure what the error could be.

My tables are table and tableTwo. "word" and "IDWord" are fields in the database, in table and tableTwo respectively. "word" is also an attribute of the class, but I have created a variable form of it to keep it in line with previous section of my code. The error is "java.sql.SQLException: not implemented by SQLite JDBC driver" and appears on the line "ResultSet result = pstmt.executeQuery(sql);"

//Outputs the whole table
public void output() {

    String insertWord = word;
    var sql = "select * from table";
    var sqlTwo = "select * from TableTwo where IDword = ?";

    try
            (// create a database connection
             Connection connection = DriverManager.
getConnection
("jdbc:sqlite:sample.db");
             Statement statement = connection.createStatement();
             PreparedStatement pstmt = connection.prepareStatement(sql);) {

        statement.setQueryTimeout(30);  // set timeout to 30 sec.

        //pstmt.setString(1, insertWord);

        ResultSet result = pstmt.executeQuery(sql);
        while (result.next()) {
            // read the result set
            System.
out
.println("name = " + result.getString("name"));
            System.
out
.println("ID = " + result.getInt("ID"));
            System.
out
.println("word = " + result.getString("word"));
        }

        ResultSet resultsTwo = pstmt.executeQuery(sql);
        while (resultsTwo.next()) {
            System.
out
.println("IDWord = " + resultsTwo.getString("IDWord"));
            System.
out
.println("num = " + resultsTwo.getInt("num"));
        }

    } catch (SQLException e) {
        // if the error message is "out of memory",
        // it probably means no database file is found
        e.printStackTrace(System.
err
);
    }
    System.
out
.println("Outputted");
}

r/javahelp • • Mar 01 '26

Binary search tree in order traversal help

0 Upvotes

**I'm very new to BST so please cut me some slack if this is a stupid question. My homework says**:

private String inOrderTraversal(BSTNode node)

Description
Traverses the BST in the in-order manner. Records the index and data values as it traverses.

Output
A string that shows a list of indices and data values, with the following formatting. It should call BSTNode.toString() to add each node’s information in a new line.

[Example output]

index: a, data: 2

index: b, data: 3

index: d, data: 1

index: g, data: 4

Right now I'm not concerned about the formatting of the output and mainly just trying to understand how to traverse a BST in order and how to implement it right. My current code is:

public class BST<I, T>{

    class BSTNode {
        private I index;
        private T data;
        private BSTNode left;
        private BSTNode right;

        
/**
         * Default constructor. Sets all instance variables to be null.
         */
        
public BSTNode() {
            // 
TODO
            
index = null;
            data = null;
            left = null;
            right = null;
        }

        
/**
         * Constructor. Sets data and index to be _data and _index respectively.
         */
        
public BSTNode(I _index, T _data) {
            // 
TODO
            
index = _index;
            data = _data;
        }

        
/**
         * Returns the index stored in this node.
         */
        
public I getIndex() {
            // 
TODO
            
return null;
        }

        
/**
         * Returns the data stored in this node.
         */
        
public T getData() {
            // 
TODO
            
return data;
        }

        
/**
         * Updates the data in this node to the specified value.
         */
        
public void setData(T d) {
            // 
TODO
            
data = d;
        }

        
/**
         * Returns a string representation of the node, indicating its index and data.
         */
        
public String toString() {
            // 
TODO
            
return "index:\t" + index.toString() + ",\t" + "data:\t" + data.toString() + "\n";
        }
    }


    private BSTNode root;
    private int size;

    
/**
     * Constructor. Initializes an empty BST with root set to null and size set to 0.
     */
    
public BST() {
        // 
TODO
        
root = null;
        size = 0;
    }


    
/**
     * Performs an in-order traversal of the BST and records indices and data values.
     */
    
private String inOrderTraversal(BSTNode node) {
        // 
TODO
        
if (node != null){
            inOrderTraversal(node.left);
        } else {
            inOrderTraversal(node.right);
            if (node == null){
                ..?
            }
        }
        return null;
    }

According to the lecture, you have to go down the left of the tree for the smallest node until you hit a node where its left is null. Then, you look to the right, and if thats also null, then you go back to the node above it to check its left but I'm not sure how to do that, or if I'm even approaching this properly. Any help is appreciated, thanks.


r/javahelp • • Dec 01 '25

Need to Choose IDE for Java Fullstack Development!!

0 Upvotes

Hi Everyone,

I have started learning Java Fullstack development. Just wanted to know if the Industry is still using Eclipse as an IDE? Because Google has stopped it's support for eclipse around 2015.

So, should I go with Eclipse IDE or choose Visual Studio Code instead?

Need an answer from Industry experts please.


r/javahelp • • Apr 16 '21

Unsolved I would like to know: why is there automatic conversion from string to int in Java when using the + operator?

2 Upvotes

You can see an example in the following video (second Java example)

https://youtu.be/NcIyV3pR1SQ


r/javahelp • • Sep 16 '22

Intellij is super confusing

0 Upvotes

I've been having difficulty making java applications in IntelliJ , even something as simple as a Hello World. I'm trying to figure out why this message "java: class HelloMarsApp is public, should be declared in a file named Hello MarsApp.java" keep appearing when I build it. I have the whole project saved in a folder name "HelloMarsApp.java, and have had no luck building it. any help is appreciated


r/javahelp • • Apr 12 '24

What can you use Spring for besides building websites?

0 Upvotes

Title. Can't make video games, can't make desktop GUIs, so what can you do?


r/javahelp • • Jan 30 '23

Explain me = and ==

0 Upvotes

Guys, I am learning Java for some time and have grasp of basics already. But one thing, which seems pretty simple hurts my brain. Please explain like you would explain it to a kid. When you use = and when you would use ==.

PS. I watch online videos and when I am sure to use =, I see them use ==. And whenever I am sure that == is needed I see them use =.


r/javahelp • • Jul 12 '22

Fully remove Java from Mac

2 Upvotes

Hi for some technical reasons I want to remove Java from my Mac and I followed every tutorial but when I write in Terminal java - version, it still appears..... Any ideas?


r/javahelp • • Jul 16 '19

Can every dp problem be solved top down and bottom up?

2 Upvotes

Title


r/javahelp • • May 01 '20

Java Problem

2 Upvotes

Create a method called process that takes in as input a String and returns a String. The method will break the String on the letter ‘e’ and move all letters after the ‘e’ to be the beginning of a new string and any letters before the ‘e’ to be the end of the String. Any String given to the method is guaranteed to have exactly one occurrence of the letter ‘e’. For example: The method would take the String “Bobehi” would return “hiBob”.

Can someone help me with this please. a simple why of doing this would be great. Thanks


r/javahelp • • Feb 07 '14

Because I can't be in a sorority and also the dumbest person in my computer science class.

0 Upvotes

It's too cliche.

I'm writing a program to keep up with a baseball team and I'm having difficulty with arrays. Right now I have a solid Driver, Team & Player class, but I can't access the individual batting averages in Player to compute the team's average in Team. Also it's due at midnight. Seriously, it's like the rules of feminism or something.

Edit (that's how I do this, right?): I've submitted my project. It definitely wasn't perfect, but at least I'm not the worst student in the class! Suck it, lesser students. I'll settle for 50th percentile for sure as long as I'm not the stereotypical sorority girl. I seriously want to learn how to program, seriously. If you're bored and want show me how the masters would do this program, go ahead. Thanks for all your help!


r/javahelp • • Sep 16 '24

java Springboot Developer 🖥️

2 Upvotes

Hi any one is here to guide me. How I can boost my career as java Springboot Developer. Currently I have 4yr of experience in mnc. I want a big changes in career. So my salary will we be 40 50 lac per annum in product based company.🙃

Suggest me if any one have idea. How I can lead my life from here.😊


r/javahelp • • May 14 '26

arraylist vs list

2 Upvotes

pls help, lets say i need an array of list.

for what purposes would i use an arraylist<string> vs a string[ ]

thanks


r/javahelp • • Feb 26 '26

Frustration with Eclipse

2 Upvotes

I'm learning to program in Java. My Eclipse IDE has a terrible problem; it won't read files unless it's given an absolute path. This is awful because an absolute path requires me to specify the file's address from the C drive, but since I'm going to send the file to someone who isn't a programmer to test, this is terrible. I want Eclipse to read a ".png" file that's in a folder next to the folder with packages and code, but it only does so if I give it the path from the C drive and doesn't accept a relative path. I can't find anything about this, and the AI ​​solutions I've tried to help have also failed.