Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9001223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:24:27+00:00 2026-06-16T00:24:27+00:00

Why this code throws out null pointer exception? I’m a beginner in java so

  • 0

Why this code throws out null pointer exception? I’m a beginner in java so i’m not familiar with this mistake, i read about it, but i wasn’t able to remove the bug … I want to multiply Complex matrix DFT with vector.

public class Naloga {
    /**
     * @param args
     */
    public static void main(String[] args) {
        double polinom1[] = {1,1,0,1};
        double polinom2[] = {1,1,0,1};
        int n=4;
        //System.out.println(n);
        int newN=2*n-1;
        //zapis matrike dft
        Complex dft[][]=new Complex [newN][newN];
        Complex omega = new Complex(Math.cos((2*Math.PI)/newN),Math.sin((2*Math.PI)/newN));
        for (int j=0;j<newN;j++){
            for (int k=0;k<n;k++){
                dft[j][k] = omega.pow((j*k)%newN);
                //System.out.println(dft[j][k]);
            }
        }
        //System.out.println("To je pol1:");
        //dopolnitev do n2
        double pol1[]=new double[newN];
        for (int i=0;i<newN;i++){
            if (i<n){
                pol1[i]=polinom1[i];
            }
            else{
                pol1[i]=0;
            }
            //System.out.println(pol1[i]);
        }
        //mnozenje polinoma z dft
        Complex p1[] = new Complex[newN];
        for (int i=0;i<newN;i++){
            Complex sum = new Complex(0,0); 
            for(int k=0;k<n;k++){
                for(int j=0;j<newN;j++){
                    sum=sum.plus(dft[i][j].times(pol1[k]));
                    System.out.println(sum);
                }
            }
            p1[i]=sum;
            System.out.println(p1[i]+" ");
        }
    }
}
class Complex{
    double re;
    double im;

    public Complex(double real, double imag) {
        re = real;
        im = imag;
    }

    public String toString() {
        double tRe = (double)Math.round(re * 100000) / 100000;
        double tIm = (double)Math.round(im * 100000) / 100000;
        if (tIm == 0) return tRe + "";
        if (tRe == 0) return tIm + "i";
        if (tIm <  0) return tRe + "-" + (-tIm) + "i";
        return tRe + "+" + tIm + "i";
    }

    // sestevanje 
    public Complex plus(Complex b) {
        Complex a = this;             
        double real = a.re + b.re;
        double imag = a.im + b.im;
        return new Complex(real, imag);
    }

    // odstevanje
    public Complex minus(Complex b) {
        Complex a = this;
        double real = a.re - b.re;
        double imag = a.im - b.im;
        return new Complex(real, imag);
    }

    // mnozenje z drugim kompleksnim stevilo
    public Complex times(Complex b) {
        Complex a = this;
        double real = a.re * b.re - a.im * b.im;
        double imag = a.re * b.im + a.im * b.re;
        return new Complex(real, imag);
    }

    // mnozenje z realnim stevilom
    public Complex times(double alpha) {
        return new Complex(alpha * re, alpha * im);
    }

    // reciprocna vrednost kompleksnega stevila
    public Complex reciprocal() {
        double scale = re*re + im*im;
        return new Complex(re / scale, -im / scale);
    }

    // deljenje
    public Complex divides(Complex b) {
        Complex a = this;
        return a.times(b.reciprocal());
    }

    // e^this
    public Complex exp() {
        return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
    }


    //potenca komplesnega stevila
    public Complex pow(int k) {

        Complex c = new Complex(1,0);
        for (int i = 0; i <k ; i++) {
            c = c.times(this);
        }
        return c;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:24:28+00:00Added an answer on June 16, 2026 at 12:24 am

    You are using more of dft than you initialize. If you change the initialization to:

        for (int j=0;j<newN;j++){
            for (int k=0;k<newN;k++){
                dft[j][k] = omega.pow((j*k)%newN);
                //System.out.println(dft[j][k]);
            }
        }
    

    the program runs to termination. Of course, it is possible that the bug is actually in how you are using dft, and you should only need an n by newN rectangle of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code throws a null pointer exception at line 20, where the compareTo() is
I have this piece of code: if(($op == q)); then which throws out this
Hi I have below pseudo code with throws an exception like this throw new
I get this error : java.net.SocketTimeoutException: Read timed out http://api.trove.nla.gov.au/result?key=<KEY HERE>&zone=all&reclevel=brief&q=%20am%20only%20yours%20fear&encoding=json Exception in thread
Why does the code throw null pointer Exception and what is the meaning of
I have this line of code but it throws an error. What is the
Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
I am writing code that catches this OutOfMemoryException and throws a new, more intuitive
Why does this code throw an Invalid Operation Exception? private SqlCommand cmd; // initialized
This code does not throw an error but the query fails, that is, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.