Isa
Isa

Reputation: 248

StackOverflowException in Json to Object / String[]

I've been reading for a while now how to do this, but i never find the awnser i understand or works.

This is my code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Bson;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Utilities;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;

namespace SongTunes
{
public partial class Form1 : Form
{
    Object Songs;

    public class Song
    {
        public int song_id { get { return song_id; } set { song_id = value; } }
        public string sorting_number { get; set; }
        public string name { get; set; }
        public string description { get; set; }
        public string lyrics { get; set; }
        public string purchase_link { get; set; }
        public string youtube_id { get; set; }
        public string has_music_video { get; set; }
        public string allow_downloads { get; set; }
        public string raw_artist_id { get; set; }
        public string artist_id { get; set; }
        public string album_artist_id { get; set; }
        public string release_date { get; set; }
        public string artist_name { get; set; }
        public string album_artist_name { get; set; }
        public string album { get; set; }
        public string bitrate { get; set; }
        public string duration { get; set; }
        public string filesize { get; set; }
        public string path { get; set; }
        public string is_part_of_compilation { get; set; }
        public string visible { get; set; }
        public string track_number { get; set; }
        public string is_explicit { get; set; }
    }

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        button1.Text = "Loading...";
        backgroundWorker1.RunWorkerAsync();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void backgroundWorker1_Done(object sender, RunWorkerCompletedEventArgs e)
    {
        button1.Enabled = true;
        button1.Text = "reload";
    }


    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string address = "";
        System.Net.WebClient webclient = new WebClient();
        string json = webclient.DownloadString(address);
        Songs = JsonConvert.DeserializeObject<List<Song>>(json);
    }

    private void SongList_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
}

It just crashes on the first

public int song_id { get { return song_id; } set { song_id = value; } }

with "An unhandled exception of type 'System.StackOverflowException' occurred in SongTunes.exe"

Upvotes: 0

Views: 792

Answers (1)

Peter
Peter

Reputation: 27944

 public int song_id { get { return song_id; } set { song_id = value; } }

The get and refers to it self, this will result in a stack overflow when you calling it. If you want to save the value in a field try:

 private int _song_id;
 public int song_id { get { return _song_id; } set { _song_id = value; } }

or just change it into:

 public int song_id { get; set; }

like your other properties

Upvotes: 4

Related Questions