This is a part of my code where it converts a binary to a decimal. The constructor should take a String parameter, and convert that to an int array. The ConvertBinToDec should then take the int array and convert it to decimal. The other picture is part of the main, where user inputs the string. The code is not working and giving an error of Exception in thread "main" java.long.NullPointerException. Please tell me how can I solve this. The user should input a binary such as "111" and the decimal value that should be printed is 7. public Binary(String binS) { String[] t = binS.split(""); for (int i = 0; i < t.length; i++) { bin[i] = Integer.parseInt(t[i]); } } public int getDec() { convertBinToDec(bin); return dec; } // convert the bin array to decimal value private int convertBinToDec(int[] bin) { int num = 1; for(int i = bin.length - 1; i >= 0; i-- ) { if (bin[i] == 1) { dec += num; } num = num * 2; } return 0; } Main: System.out.print("Enter binary:"); String line = keyboardInput.next(); Binary b2 = new Binary(line); System.out.println(b2.getDec());
This is a part of my code where it converts a binary to a decimal. The constructor should take a String parameter, and convert that to an int array. The ConvertBinToDec should then take the int array and convert it to decimal. The other picture is part of the main, where user inputs the string. The code is not working and giving an error of Exception in thread "main" java.long.NullPointerException. Please tell me how can I solve this.
The user should input a binary such as "111" and the decimal value that should be printed is 7.
public Binary(String binS) {
String[] t = binS.split("");
for (int i = 0; i < t.length; i++) {
bin[i] = Integer.parseInt(t[i]);
}
}
public int getDec() {
convertBinToDec(bin);
return dec;
}
// convert the bin array to decimal value
private int convertBinToDec(int[] bin) {
int num = 1;
for(int i = bin.length - 1; i >= 0; i-- ) {
if (bin[i] == 1) {
dec += num;
}
num = num * 2;
}
return 0;
}
Main:
System.out.print("Enter binary:");
String line = keyboardInput.next();
Binary b2 = new Binary(line);
System.out.println(b2.getDec());
![// Fill in the constructor below
// Make sure that dec value to be properly calculated
public Binary(String bins) {
String[] t =
bins.split( regex: "");
for (int i = ®; i < t.length; i++) {
bin[i] = Integer.parseInt(t[i]);
public int getDec() {
convertBinToDec(bin);
return dec;
}
// convert the bin array to decimal value
private int convertBinToDec(int[] bin) {
int num = 1;
for(int i
= bin.length
1; i >= ®; i-- ) {
if (bin[i] == 1) {
dec += num;
}
num = num * 2;
return 0;](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F47172a01-dc9c-43a5-97e6-7fd3dbf42a8c%2Fd3773cf9-182e-4313-8d86-f2705998706f%2Flma8m4q_processed.png&w=3840&q=75)
![System.out.print("Enter binary:");
String line
keyboardInput.next();
Binary b2
= new
Binary (line);
System.out.println(b2.getDec());](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F47172a01-dc9c-43a5-97e6-7fd3dbf42a8c%2Fd3773cf9-182e-4313-8d86-f2705998706f%2Fqtlzo4a_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)